我有表「學生」SQL update語句
P_ID LastName FirstName Address City
1 Hansen Ola
2 Svendson Tove
3 Petterson Kari
4 Nilsen Johan
...and so on
如何更改編輯代碼在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();
}
編輯條目,其中姓字段姓價值姓氏字段具有名字值。
我不想使用這樣
UPDATE Persons SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
,我編輯的我原來的語句
command.CommandText = "UPDATE Student(LastName, FirstName, Address, City)
VALUES (@ln, @fn, @add, @cit) WHERE LastName='" + lastName +
"' AND FirstName='" + firstName+"'";
但聲明是沒有得到執行,爲什麼會拋出SQL異常?有沒有解決它的辦法?
它拋出什麼異常? – 2013-03-06 11:31:03
錯誤的語法附近'(' – 2013-03-06 11:51:12
,這是正確的,因爲你有錯誤的語句到位。更新TABLE_NAME SET ...應該是正確的語法..檢查我的帖子在答案找到正確的方式。 – 2013-03-06 11:52:57