這不會在我的數據庫表中更新。我過分看了一些東西嗎?更新SqlCommand-不在數據庫中更新
這些值在文本框中很好。沒有錯誤顯示,很奇怪。
using (SqlConnection connection = new SqlConnection(@"Data Source = UKMAN1NB10038\SQLEXPRESS; Initial Catalog = TheVets; Integrated Security = True"))
{
SqlCommand command = new SqlCommand("UPDATE OwnerTable SET Owner_Fname [email protected] , Owner_Lname = @OwnerLname, Owner_HouseNo = @OwnerHouse, Owner_Street = @OwnerStreet, Owner_County = @OwnerCounty, Owner_PostCode = @OwnerPost, Owner_Tele = @OwnerTele, Owner_Email = @OwnerEmail WHERE Owner_ID = '" + CB_EDIT_OWNER.SelectedText + "'", connection);
command.CommandType = CommandType.Text;
command.Connection = connection;
command.Parameters.AddWithValue("@OwnerFname", TXT_EDIT_FNAME.Text);
command.Parameters.AddWithValue("@OwnerLname", TXT_EDIT_LNAME.Text);
command.Parameters.AddWithValue("@OwnerHouse", TXT_EDIT_HOUSE.Text);
command.Parameters.AddWithValue("@OwnerStreet", TXT_EDIT_STREET.Text);
command.Parameters.AddWithValue("@OwnerCounty", TXT_EDIT_COUNTY.Text);
command.Parameters.AddWithValue("@OwnerPost", TXT_EDIT_POSTCODE.Text);
command.Parameters.AddWithValue("@OwnerTele", TXT_EDIT_TELE.Text);
command.Parameters.AddWithValue("@OwnerEmail", TXT_EDIT_EMAIL.Text);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
爲什麼你爲所有的東西使用參數,但是你使用字符串添加爲'CB_EDIT_OWNER.SelectedText'?爲什麼這不是一個參數呢? (很可能owner_id不是參數是問題的根源) –
可能是ID ...檢查它的運行時間值。並且考慮將其作爲參數添加,對於這樣的腳本來說可能並不安全。 'command'也應該使用(){}包裝,就像連接一樣。 – dlatikay
使用調試器,看看'CB_EDIT_OWNER.SelectedText'返回 –