我想用c#更新數據庫im上的用戶信息。 這是我的代碼,它不工作,它不會給我任何錯誤。更新數據庫中的信息
protected void Page_Load(object sender, EventArgs e)
{
lbldisplay.Text = "<b><font color=BLUE>" + "WELLCOME:: " + "</font>" + "<b><font color=white>" + Session["Name"] + "</font>";
SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=webservice_BuyBid;Integrated Security=True");
}
protected void btnLogOut_Click(object sender, EventArgs e)
{
//this will redirect the page to the home.aspx page.
Response.Redirect("Home.aspx");
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
//creating a new connection to the database
SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=True;");
con.Open();
//creates a new sqlcommand to update the buyer's information to the Database.
SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email [email protected],CellNumber [email protected]", con);
con.Open();
cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text);
cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text);
cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text);
int rows = cmd.ExecuteNonQuery();
con.Close();
不相關,但我建議EntityFramework,而不是使用SQL API與您自己的查詢。 – mirichan
你用SQL Server Profiler跟蹤了你的查詢嗎?是否有一些命令實際上被髮送到服務器?注意:你正在使用'update'而沒有'where'條件 - 所以你會將表中的所有**行更新爲相同的值。 –
我認爲歐普應該首先闡述「它不起作用」的部分。它究竟如何「不起作用」? @op如果你沒有得到任何錯誤,那麼也許代碼**會**工作,而不是你想要的方式。 – Banana