Instruction de mise à jour SQL en C #

J’ai la table “Etudiant”

P_ID LastName FirstName Address City 1 Hansen Ola 2 Svendson Tove 3 Petterson Kari 4 Nilsen Johan ...and so on 

Comment changer le code d’édition en C #

  ssortingng firstName = "Ola"; ssortingng lastName ="Hansen"; ssortingng address = "ABC"; ssortingng city = "Salzburg"; ssortingng connectionSsortingng = System.Configuration.ConfigurationManager .ConnectionSsortingngs["LocalDB"].ConnectionSsortingng; using (SqlConnection connection = new SqlConnection(connectionSsortingng)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "INSERT INTO Student (LastName, FirstName, Address, City) VALUES (@ln, @fn, @add, @cit)"; command.Parameters.AddWithValue("@ln", lastName); command.Parameters.AddWithValue("@fn", firstName); command.Parameters.AddWithValue("@add", address); command.Parameters.AddWithValue("@cit", city); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } 

pour éditer une entrée où le champ Nom a la valeur nom et le champ Prénom nom a la valeur prénom.

Je ne veux pas utiliser comme ça

  UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob' 

et j’ai édité ma déclaration originale à

  command.CommandText = "UPDATE Student(LastName, FirstName, Address, City) VALUES (@ln, @fn, @add, @cit) WHERE LastName='" + lastName + "' AND FirstName='" + firstName+"'"; 

mais l’instruction n’est pas en cours d’exécution, pourquoi renvoie-t-elle une exception SQL? Y at-il aucune solution à cela?

Ce n’est pas une méthode correcte pour mettre à jour un enregistrement en SQL:

 command.CommandText = "UPDATE Student(LastName, FirstName, Address, City) VALUES (@ln, @fn, @add, @cit) WHERE LastName='" + lastName + "' AND FirstName='" + firstName+"'"; 

Vous devriez l’écrire comme ceci:

 command.CommandText = "UPDATE Student SET Address = @add, City = @cit Where FirstName = @fn and LastName = @add"; 

Ensuite, vous ajoutez les mêmes parameters que vous les avez ajoutés pour l’opération d’insertion.

Je ne veux pas utiliser comme ça

C’est la syntaxe de l’instruction Update dans SQL , vous devez utiliser cette syntaxe sinon vous obtiendrez une exception.

 command.Text = "UPDATE Student SET Address = @add, City = @cit Where FirstName = @fn AND LastName = @ln"; 

puis ajoutez vos parameters en conséquence.

 command.Parameters.AddWithValue("@ln", lastName); command.Parameters.AddWithValue("@fn", firstName); command.Parameters.AddWithValue("@add", address); command.Parameters.AddWithValue("@cit", city); 

Il y a toujours une syntaxe appropriée pour chaque langue. De même, SQL (Structured Query Language) a également une syntaxe spécifique pour la requête de mise à jour que nous devons suivre si nous voulons utiliser la requête de mise à jour. Sinon, cela ne donnera pas les résultats escomptés.

 ssortingng constr = @"Data Source=(LocalDB)\v11.0;Initial Catalog=Bank;Integrated Security=True;Pooling=False"; SqlConnection con = new SqlConnection(constr); DataSet ds = new DataSet(); con.Open(); SqlCommand cmd = new SqlCommand(" UPDATE Account SET name = Aleesha, CID = 24 Where name =Areeba and CID =11 )"; cmd.ExecuteNonQuery(); 

Si vous ne souhaitez pas utiliser la syntaxe SQL (ce que vous êtes obligé de faire), passez à un framework tel que Entity Framework ou Linq-to-SQL où vous n’écrivez pas vous-même les instructions SQL.

 command.Text = "UPDATE Student SET Address = @add, City = @cit Where FirstName = @fn and LastName = @add"; 

S’il vous plaît, n’utilisez jamais cette forme concat:

 Ssortingng st = "UPDATE supplier SET supplier_id = " + textBox1.Text + ", supplier_name = " + textBox2.Text + "WHERE supplier_id = " + textBox1.Text; 

utilisation:

 command.Parameters.AddWithValue("@atsortingbute", value); 

Toujours travailler orienté object

Edit: En effet, lorsque vous paramétrez vos mises à jour, cela évite les injections SQL.

 private void button4_Click(object sender, EventArgs e) { Ssortingng st = "DELETE FROM supplier WHERE supplier_id =" + textBox1.Text; SqlCommand sqlcom = new SqlCommand(st, myConnection); try { sqlcom.ExecuteNonQuery(); MessageBox.Show("刪除成功"); } catch (SqlException ex) { MessageBox.Show(ex.Message); } } private void button6_Click(object sender, EventArgs e) { Ssortingng st = "SELECT * FROM suppliers"; SqlCommand sqlcom = new SqlCommand(st, myConnection); try { sqlcom.ExecuteNonQuery(); SqlDataReader reader = sqlcom.ExecuteReader(); DataTable datatable = new DataTable(); datatable.Load(reader); dataGridView1.DataSource = datatable; //MessageBox.Show("LEFT OUTER成功"); } catch (SqlException ex) { MessageBox.Show(ex.Message); } } 
 Ssortingng st = "UPDATE supplier SET supplier_id = " + textBox1.Text + ", supplier_name = " + textBox2.Text + "WHERE supplier_id = " + textBox1.Text; SqlCommand sqlcom = new SqlCommand(st, myConnection); try { sqlcom.ExecuteNonQuery(); MessageBox.Show("update successful"); } catch (SqlException ex) { MessageBox.Show(ex.Message); }