Bonsoir,
Sous visual studio afin de mieux comprendre le fonctionnement d'une base de données j'ai créé une Form avec une textBoxNum, une textBoxNom et une textBoxMdp et pour le code j'ai mis
Le principe est simple, on marque un truc dans les textes box, on appuie sur le bouton et ça ajoute ce qu'on a marqué dans la table créée avec phpmyadmin en local.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlConnection maConnexion = new SqlConnection("Data Source=localhost;Initial Catalog=bdtest;User Id=test;Password=test;"); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlCommand maCommande = new SqlCommand(); maCommande.Connection = maConnexion; maConnexion.Open(); maCommande.CommandText = "INSERT INTO tbltest (fldnum, fldnom, fldmdp) VALUES ("+ textBoxNum.Text + ", '" + textBoxNom.Text + ", "+ textBoxMdp.Text +"');"; maConnexion.Close(); } } }
Le problème c'est que ça me dit que la base de donnée est inexistante ou injoignable.
J'ai aussi testé avec user id = localhost mais ça ne fonctionne pas (localhost n'a pas de mot de passe).
Que dois-je faire ?
J'ai également testé :
Mais j'ai le message :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlConnection maConnexion = new SqlConnection("Data Source=localhost,1027 ;Initial Catalog=bdtest;User Id=test;Password=test;"); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlCommand maCommande = new SqlCommand(); maCommande.Connection = maConnexion; maConnexion.Open(); maCommande.CommandText = "INSERT INTO tbltest VALUES (@maValeur1, @maValeur2, @maValeur3);"; // Puis on définit les valeurs maCommande.Parameters.Add(new SqlParameter("@maValeur1", textBoxNum.Text)); maCommande.Parameters.Add(new SqlParameter("@maValeur2", textBoxNom.Text)); maCommande.Parameters.Add(new SqlParameter("@maValeur2", textBoxMdp.Text)); maConnexion.Close(); } } }
Une connexion a été établie avec le serveur, mais une erreur s'est ensuite produite pendant la négociation préalable à l'ouverture de session. (provider: TCP Provider, error: 0 - Une connexion existante a dû être fermée par l'hôte distant.)
Merci de votre aide.
Partager