我有註冊爲我的數據庫表。我想將我的信息更新到我的SQL Server數據庫中。但它不起作用,它不會發生任何錯誤,但數據鍵不會更新到我的數據庫中。有人請幫我解決我的代碼有什麼問題嗎?謝謝。更新SQL Server數據庫中的數據
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UPDATE registration SET username = @username, password = @password, retypepassword = @retypepassword, gender = @gender, birth = @birth, address = @address, city = @city, country = @country, postcode = @postcode, email = @email, carno = @carno", con);
con.Open();
cmd.Parameters.AddWithValue("@username", TextBoxUsername.Text);
cmd.Parameters.AddWithValue("@password", TextBoxPassword.Text);
cmd.Parameters.AddWithValue("@retypepassword", TextBoxRPassword.Text);
cmd.Parameters.AddWithValue("@gender", DropDownListGender.Text);
cmd.Parameters.AddWithValue("@birth", DropDownListDay.Text);
cmd.Parameters.AddWithValue("@address", TextBoxAddress.Text);
cmd.Parameters.AddWithValue("@city", TextBoxCity.Text);
cmd.Parameters.AddWithValue("@country", DropDownListCountry.Text);
cmd.Parameters.AddWithValue("@postcode", TextBoxPostcode.Text);
cmd.Parameters.AddWithValue("@email", TextBoxEmail.Text);
cmd.Parameters.AddWithValue("@carno", TextBoxCarno.Text);
cmd.ExecuteNonQuery();
con.Close();
if (IsPostBack)
{
Response.Redirect("UpdateSuccess.aspx");
}
後,我點擊確認它在某種程度上只更新我的專欄gender
從男性到女性,數據的其他列不會更新。
表中的主鍵是什麼? 你可以添加一個try/catch並查看是否有異常? –
命令在發送到服務器時的外觀如何?如果您有完整版本,請使用分析器來檢查發送到服務器的內容。 – alzaimar
這似乎是零感。認真。對於USERNAME,您不需要更新,因爲您也可以在where子句中使用它。 Set name = a where name = a?永遠不要更新主鍵。那麼,也給我們錯誤消息。 – TomTom