1. SqlCommand maCommande = new SqlCommand(); maCommande.Connection = maConnexion; maCommande.CommandText = "INSERT INTO Table (monChamp1, monChamp2) VALUES (" + monInt + ", '" + monstring + "');";
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Sql ; using System.Data.SqlClient ; namespace PAYS { //Tu commence tout d abord a créer une classe //pour ta conexion class REX_cnx { public string req; public SqlDataReader rdr; SqlCommand cmd; SqlDataAdapter adpt; SqlConnection con; string strcon; public REX_cnx () { strcon = "Data Source=.\\SQLEXPRESS;Initial Catalog=link001;Integrated Security=True"; } public void connexion() { con = new SqlConnection(strcon); con.Open(); cmd = new SqlCommand(req,con); adpt = new SqlDataAdapter(cmd); rdr = cmd.ExecuteReader(); } public void decon() { rdr.Dispose(); con.Dispose(); } } } //------------------------------------------------------------ //ici c est juste les idées que tu peut //essayer pour une seule table using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Data.Sql; using System.Data.SqlClient; using System.Windows.Forms; namespace PAYS { public partial class Form1 : Form { //SqlConnection cnx = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=link001;Integrated Security=True"); REX_cnx kat = new REX_cnx(); SqlDataAdapter adt; SqlCommand cd; SqlDataReader rd; string strcon; public Form1() { InitializeComponent(); } private void enregistrer_Click(object sender, EventArgs e) { try { kat.req = "Insert Into Pays(codep,lib_p,capitale,hymn,sup,lang,codpost) Values('" + pay.Text + "','" + libp.Text + "','" + cap.Text + "','" + hym.Text + "','" + sup.Text + "','" + lang.Text + "','" + codpost.Text + "') "; kat.connexion(); kat.decon(); pay.Text = ""; libp.Text = ""; cap.Text = ""; hym.Text = ""; sup.Text = ""; lang.Text = ""; codpost.Text = ""; pay.Focus(); MessageBox.Show("ok"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { kat.req = "select * from Pays order by codep asc"; kat.connexion(); PAYSbox.Items.Clear(); while (kat.rdr.Read()) { PAYSbox.Items.Add(kat.rdr.GetValue(0)); } kat.decon(); //------------------------------------------- // cnx.Open(); // SqlCommand cdd = new SqlCommand(); // cdd.Connection = cnx; // cdd.CommandText = "select * from Pays order by codep asc"; // rd = cdd.ExecuteReader(); // while (rd.Read()) // { // PAYSbox.Items.Add(rd["codep"]); // } // rd.Close(); } private void PAYSbox_SelectedIndexChanged(object sender, System.EventArgs e) { } private void buttonMOD_Click(object sender, EventArgs e) { //REX Mindongo vérifie que le Combobox n'est pas vide if (PAYSbox.Text == "") { MessageBox.Show("Impossible de trouver les résultats souhaité,Veuillez selectionner le Code pays svp.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { kat.req "Update Pays set lib_p'" + libp.Text + "',capitale='" + cap.Text + "',hymn='" + hym.Text + "',sup='" + sup.Text + "',lang='" + lang.Text + "',codpost='" + codpost.Text + "' where codep='" + PAYSbox.SelectedItem + "'"; kat.connexion(); kat.decon(); pay.Text = ""; libp.Text = ""; cap.Text = ""; hym.Text = ""; sup.Text = ""; lang.Text = ""; codpost.Text = ""; pay.Focus(); MessageBox.Show("ok"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void RECHERCHE_Click(object sender, EventArgs e) { //REX Mindongo vérifie que le Combobox n'est pas vide if (PAYSbox.Text == "") { MessageBox.Show("Impossible de trouver les résultats souhaité,Veuillez selectionner le Code pays svp.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } kat.req = "select * from pays where codep='"+PAYSbox.Text+"'"; kat.connexion(); while (kat.rdr.Read()) { //pay.Text = kat.rdr.GetString(); libp.Text = kat.rdr.GetString(1); ; cap.Text = kat.rdr.GetString(2);; hym.Text = kat.rdr.GetString(3); ; sup.Text = kat.rdr.GetString(4); ; lang.Text = kat.rdr.GetString(5); ; } kat.decon(); } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionstring str = "select * from imp where Matricule =" + comboBox1.SelectedItem;dejà là
string str "select * from imp where Matricule'"+comboBox1.SelectedItem+"'";
while (dr.Read()) {//il te faut cimmencer par zéro textBox1.Text = (dr.GetString(0));//dr[0].ToString(); textBox2.Text =(dr.GetString(1)); textBox3.Text = (dr.GetString(2)); textBox4.Text = (dr.GetString(3))); textBox5.Text = (dr.GetString(4))(); textBox6.Text = (dr.GetString(5)); textBox7.Text = (dr.GetString(6))(); textBox8.Text = (dr.GetString(7)); textBox9.Text = (dr.GetString(8)); textBox10.Text = (dr.GetString(9)); textBox11.Text = (dr.GetString(10)); } else { MessageBox.Show("ce ...... n'existe pas"); }
(dr.GetString(0)); //car ca dépas de tes champs char, numeric...... si tout ne le sont pas alors tu peut modifier
con.Open(); string str "select * from imp where Matricule'" + textBox13.Text.ToString() + "'"; //si possible tu peut lancer ce qui suit pour vérifier dans ta bdd //string str = ("insert into affectation_des_imp ("Matricule") values('" +textBox13.Text+ "'); SqlCommand cmd = new SqlCommand(str, con); dr = cmd.ExecuteReader(); if (dr.read()) { MessageBox.Show(this, "....Existant", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { dr.Close(); string s = ("insert into affectation_des_imp values('" +textBox13.Text.ToString() + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "')"); cmd.ExecuteNonQuery() MessageBox.Show("Ajout effectué !!"); }
if (textBox1.Text != "" && textBox2Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "")
con.Open();// pourquoi l'ouverture de la connexion????? if (textBox1.Text == "") { MessageBox.Show(" matricule vide", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
string str "select * from matériel where code_mat'" + textBox1.Text.ToString() + "'"; SqlCommand cmd = new SqlCommand(str, con); dr = cmd.ExecuteReader(); if (dr.Read()) { MessageBox.Show(this, " Code matériel déja Existant", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (textBox1.Text != "" && textBox2Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "") dr.Close(); string s = ("insert into matériel values('" + textBox1.Text.ToString() + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')"); SqlCommand cmd1 = new SqlCommand(s, con); cmd1.ExecuteNonQuery(); MessageBox.Show("Ajout effectué !!"); } con.Close(); }