Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL update statement in C#

Tags:

c#

sql

I have table "Student"

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

How do i change edit code in C#

 string firstName = "Ola";  string  lastName ="Hansen";  string  address = "ABC";  string city = "Salzburg";   string connectionString = System.Configuration.ConfigurationManager                           .ConnectionStrings["LocalDB"].ConnectionString;   using (SqlConnection connection = new SqlConnection(connectionString))      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();  }  

to edit entry where Lastname field has lastname value and FirstName field has firstname value.

I dont want to use like this

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

and i edited my original statement to

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

but the statement is not getting executed, why is it throwing SQL exception ? Is there nay solution to it ?

like image 522
Андрей Про Avatar asked Mar 06 '13 11:03

Андрей Про


People also ask

What is the syntax for update?

The basic SQL UPDATE syntax comes down to using keyword UPDATE followed by the name of our object (table or table alias) and the SET column name equals to some values. The FROM clause will come into play when we do joins and we can also have a WHERE clause when we need to update only a portion of data in a table.

Which method of the SQL command is used to update the record in C#?

UPDATE statement in SQL The SQL UPDATE statement is used to modify the existing data records in a table. The SQL UPDATE statement is used to update data from a database table or tables.

How do you write a record update query?

UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition; table_name: name of the table column1: name of first , second, third column.... value1: new value for first, second, third column.... condition: condition to select the rows for which the values of columns needs to be updated.


2 Answers

This is not a correct method of updating record in SQL:

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

You should write it like this:

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

Then you add the parameters same as you added them for the insert operation.

like image 69
K D Avatar answered Sep 20 '22 09:09

K D


I dont want to use like this

That is the syntax for Update statement in SQL, you have to use that syntax otherwise you will get the exception.

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

and then add your parameters accordingly.

command.Parameters.AddWithValue("@ln", lastName); command.Parameters.AddWithValue("@fn", firstName); command.Parameters.AddWithValue("@add", address); command.Parameters.AddWithValue("@cit", city);   
like image 37
Habib Avatar answered Sep 17 '22 09:09

Habib