我正在尝试更新 sql server 中的表,但它不起作用。这是我的代码
SqlConnection conn;
string connString = ConfigurationManager.ConnectionStrings["Alumnidb"].ConnectionString;
string userName;
SqlCommand cmdProfile, cmdUpdate;
SqlDataReader reader;
string UserId;
protected void Page_Load(object sender, EventArgs e)
{
userName = Request.QueryString["UserName"].ToString();
RetriveProfile();
}
protected void RetriveProfile()
{
conn = new SqlConnection(connString);
cmdProfile = new SqlCommand("SELECT Name, UserId FROM UserProfile WHERE UserName=@UserName",conn);
cmdProfile.Parameters.AddWithValue("@UserName",userName);
conn.Open();
reader = cmdProfile.ExecuteReader();
while (reader.Read())
{
TextBoxName.Text = reader["Name"].ToString();
UserId = reader["UserId"].ToString();
}
conn.Close();
}
protected void buttonUpdate_Click(object sender, EventArgs e)
{
conn = new SqlConnection(connString);
cmdUpdate = new SqlCommand("UPDATE UserProfile SET Name=@Name WHERE UserId=@UserId",conn);
cmdUpdate.Parameters.AddWithValue("@UserId",UserId);
cmdUpdate.Parameters.AddWithValue("@Name",TextBoxName.Text.ToString());
conn.Open();
cmdUpdate.ExecuteScalar();
conn.Close();
}
和 .aspx 文件
Name: <asp:TextBox ID="TextBoxName" runat="server" ></asp:TextBox>
<asp:Button ID="buttonUpdate" runat="server" Text="UpDate"
onclick="buttonUpdate_Click"/>
它向我显示了以前更新的值。. 我签入了sql server,那里也没有变化我做错了什么?您的帮助将不胜感激。. .谢谢