Mettre à jour la command Executenonquery – Erreur de syntaxe

J'ai essayé de mettre à jour certaines données en C # j'ai

"Syntaxe incorrecte près de ')'." dans la ligne "da.UpdateCommand.ExecuteNonQuery ();".

J'ai parcouru le code beaucoup de time, et je ne peux plus voir d'erreur. Donc, à mon avis devrait être OK, mais ne l'est pas. Pourriez-vous regarder le code ci-dessous?

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 klinika { public partial class frModWla : Form { DataSet ds = new DataSet(); SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=Klinika; Integrated security=TRUE"); SqlDataAdapter da = new SqlDataAdapter(); BindingSource bs = new BindingSource(); public frModWla() { InitializeComponent(); } private void btnWyswietl_Click(object sender, EventArgs e) { bindingClear(); da.SelectCommand = new SqlCommand("SELECT * FROM tbWlasciciel", cs); ds.Clear(); da.Fill(ds); dg.DataSource = ds.Tables[0]; bs.DataSource = ds.Tables[0]; tbxImie.DataBindings.Add(new Binding("Text", bs, "Imie")); tbxNazwisko.DataBindings.Add(new Binding("Text", bs, "Nazwisko")); tbxMiejscowosc.DataBindings.Add(new Binding("Text", bs, "Miejscowosc")); tbxKodPocztowy.DataBindings.Add(new Binding("Text", bs, "Kod_pocztowy")); tbxAdres.DataBindings.Add(new Binding("Text", bs, "Adres")); tbxTelefon.DataBindings.Add(new Binding("Text", bs, "Telefon")); tbxEmail.DataBindings.Add(new Binding("Text", bs, "Email")); rekord(); } private void bindingClear() { tbxImie.DataBindings.Clear(); tbxNazwisko.DataBindings.Clear(); tbxMiejscowosc.DataBindings.Clear(); tbxKodPocztowy.DataBindings.Clear(); tbxAdres.DataBindings.Clear(); tbxTelefon.DataBindings.Clear(); tbxEmail.DataBindings.Clear(); } private void btnPoprzedni_Click(object sender, EventArgs e) { bs.MovePrevious(); dgUpdate(); rekord(); } private void btnNastepny_Click(object sender, EventArgs e) { bs.MoveNext(); dgUpdate(); rekord(); } private void btnPierwszy_Click(object sender, EventArgs e) { bs.MoveFirst(); dgUpdate(); rekord(); } private void btnOstatni_Click(object sender, EventArgs e) { bs.MoveLast(); dgUpdate(); rekord(); } private void dgUpdate() { dg.ClearSelection(); dg.Rows[bs.Position].Selected = true; rekord(); } private void rekord() { lblRecords.Text = "Rekord " + bs.Position + " z " + (bs.Count - 1); } private void btnUaktualnij_Click(object sender, EventArgs e) { da.UpdateCommand = new SqlCommand("UPDATE tbWlasciciel SET NAZWISKO = @NAZWISKO, IMIE = @IMIE, MIEJSCOWOSC = @MIEJSCOWOSC, KOD_POCZTOWY = @KOD_POCZTOWY, ADRES = @ADRES, TELEFON = @TELEFON, EMAIL = @EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA)", cs); da.UpdateCommand.Parameters.Add("@IMIE", SqlDbType.VarChar).Value = tbxImie.Text; da.UpdateCommand.Parameters.Add("@NAZWISKO", SqlDbType.VarChar).Value = tbxNazwisko.Text; da.UpdateCommand.Parameters.Add("@MIEJSCOWOSC", SqlDbType.VarChar).Value = tbxMiejscowosc.Text; da.UpdateCommand.Parameters.Add("@KOD_POCZTOWY", SqlDbType.VarChar).Value = tbxKodPocztowy.Text; da.UpdateCommand.Parameters.Add("@ADRES", SqlDbType.VarChar).Value = tbxAdres.Text; da.UpdateCommand.Parameters.Add("@TELEFON", SqlDbType.VarChar).Value = tbxTelefon.Text; da.UpdateCommand.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value = tbxEmail.Text; da.UpdateCommand.Parameters.Add("@ID_WLASCICIELA", SqlDbType.Int).Value = ds.Tables[0].Rows[bs.Position][0]; cs.Open(); da.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("Dane w bazie danych zostały zaktualizowane!", "Aktualizacja danych"); cs.Close(); } } } 

Vous devez supprimer ")" à la fin de votre SqlCommand. Vous n'avez pas de début ( donc c'est vous dire que le ) à la fin est une syntaxe invalide, ce qui est le cas.

Cela ressemble à un "" dans votre clause WHERE:

 ...@EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA)", cs) 

où il devrait être:

 ...@EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA", cs) 

Supprimez simplement ) dans votre SqlCommand . Il a raison du paramètre @ID_WLASCICIELA . Vous avez ouvert les crochets une fois mais vous avez fermé les crochets deux fois.

Utilisez ceci:

 da.UpdateCommand = new SqlCommand("UPDATE tbWlasciciel SET NAZWISKO = @NAZWISKO, IMIE = @IMIE, MIEJSCOWOSC = @MIEJSCOWOSC, KOD_POCZTOWY = @KOD_POCZTOWY, ADRES = @ADRES, TELEFON = @TELEFON, EMAIL = @EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA", cs);