bonjour,
quel code me permettra de remplir une table d'une base de donnée sql server,
merci
Version imprimable
bonjour,
quel code me permettra de remplir une table d'une base de donnée sql server,
merci
Bonjour,
Ce sujet a été traité 97941979747979 de fois sur ce forum : utilise la fonction rechercher.
J'espère que tu as lu des tutoriaux sur la connection base de données!
Pour te répondre simplement je dirais qu'il te faut écrire une requête SQL ou une procédure stockée du genre
Code:"INSERT INTO MaTable (champ1, champ2) VALUES (valeur1, valeur2)"
voila, j'ai ecris ce code mais
j'ai un message comme quoi il une erreur..Code:
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 private void button1_Click(object sender, EventArgs e) { Add_informations ma_form_Add_informations = new Add_informations(); { if (textBox_project_name.Text != "" && textBox_Date.Text != "" && comboBox1.Text != "" && comboBox2.Text != "") { System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(); conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=RENNDXPRDL2596\SQLEXPRESS;Initial Catalog=hafid;Integrated Security=True"; try { conn.Open(); } catch (Exception) { MessageBox.Show("Notre connection n'est pas établit"); } finally { int r; System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand("INSERT INTO Table_Project(ID_project,Project Category,kick off date,Location,) VALUES('" + textBox_project_name.Text + "','" + textBox_Date.Text + "','" + comboBox1.Text + "','" + comboBox2.Text, conn); r=command.ExecuteNonQuery (); //MessageBox.Show ("Operation d'ajout reussie"); Add_informations f = new Add_informations(); f.Show(); } }
Code:
1
2
3
4
5
6je ne vois pas l erreur . merci ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
si tu utilises sql server comme bdd, tu dois te servir de SqlConnection et non oleDBConnection
Code:
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 private void button1_Click(object sender, EventArgs e) { Add_informations ma_form_Add_informations = new Add_informations(); //{ à quoi il sert ce { if (textBox_project_name.Text != "" && textBox_Date.Text != "" && comboBox1.Text != "" && comboBox2.Text != "") { //System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(); System.Data.Sql.SqlConnection conn = new System.Data.Sql.SqlConnection(); conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=RENNDXPRDL2596\SQLEXPRESS;Initial Catalog=hafid;Integrated Security=True"; try { conn.Open(); System.Data.Sql.SqlCommand command = new System.Data.Sql.SqlCommand("INSERT INTO Table_Project(ID_project,[Project Category],[kick off date],Location) VALUES('" + textBox_project_name.Text + "','" + textBox_Date.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "'" , conn); r=command.ExecuteNonQuery (); } catch (Exception) { MessageBox.Show("Notre connection n'est pas établit"); } finally { Add_informations f = new Add_informations(); f.Show(); } }
J'ajoute que ce n'est pas la cause réelle de l'exception :)
En fait quand tu créés un objet Command manuellement, il faut lui donner l'objet Connection.
En gros dans ton code tu as instancié ta connexion et ta commande, il faut aussi faire :
Sinon, tu peux ne pas instancier ta commande à la main :Code:
1
2 command.Connection=conn;
Voila,Code:
1
2 command=conn.CreateCommand(); // ou quelque chose comme ça
mais comme le dit fally, il vaut mieux que tu utilises les classes spécifiques à Sql Server (SqlConnection et SqlCommand).
Bonne journée
En Gros
:arrow: pour SQL Server : utiliser le namespace System.Data.Sql.*
:arrow: pour une requéte : mettre entre [] les champs de la base contenant un espace (exemple : [Project Category] et non pas Project Category)
:arrow: ne pas oublier de refermer les '
etc...
merci les amis, je vais recommence.
j'ai suivis les tot , donc j 'ai écris ce code
sachant que j'ai pas pu metre system.data.sql je ne le trouve pas dans reference..:bug:Code:
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 private void button1_Click(object sender, EventArgs e) { Add_informations ma_form_Add_informations = new Add_informations(); { // Chaîne de connexion string connectString = @"Data Source=RENNDXPRDL2596\SQLEXPRESS;Initial Catalog=hafid;Integrated Security=True"; // Objet connection SqlConnection connection = new SqlConnection(connectString); // Ouverture connection.Open(); // Objet Command SqlCommand command = new SqlCommand("INSERT INTO Table_Project([ID_project],[Project Category],[kick off date],Location) VALUES('" + textBox_project_name.Text + "','" + textBox_Date.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "');", connection); // Execution int affectedrows = command.ExecuteNonQuery(); Console.WriteLine("Nombre de lignes affectées {0}", affectedrows); // Fermeture connection connection.Close(); { } } } <div class="bbcode_container"> <div class="bbcode_description">Citation:</div> <div class="bbcode_quote printable"> <hr /> The type or namespace name 'SqlConnection' could not be found <hr /> </div> </div> } }
merci
mais ca marche pas il me di que
oui j'imagine dela ce qu'il dit...
va dans les références de ton projet, clique droit fais ajouter une référence et va prendre la référence System.Data