我有一个users
表,其中包含字段uname
,ushortname
和pswd
以及ucode
作为主键。
以下UPDATE
无效:
protected void Page_Load(object sender, EventArgs e)
{
string uname = Request.QueryString["uname"];
string ucode = Request.QueryString["ucode"];
if (!string.IsNullOrEmpty(uname))
{
SqlCommand cmd = new SqlCommand("select * from users WHERE ucode=@ucode", conn);
SqlDataAdapter dadapter = new SqlDataAdapter();
conn.Open();
txtUserName.ReadOnly = false;
txtUserShortName.ReadOnly = false;
txtPassword.ReadOnly = false;
dadapter.SelectCommand = cmd;
cmd.Parameters.Add(new SqlParameter("@ucode", ucode));
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
LblUcode.Text = dr["ucode"].ToString();
txtPassword.Text=dr["pswd"].ToString();
txtUserShortName.Text = dr["ushortname"].ToString();
txtUserName.Text = dr["uname"].ToString();
}
dr.Close();
conn.Close();
}
}
SqlCommand cmd = new SqlCommand("update users Set uname=@Uname,ushortname=@Ushortname,pswd=HASHBYTES('MD5',@Pswd) where ucode=@UCODE", conn);
cmd.Parameters.AddWithValue("@Uname", txtUserName.Text);
cmd.Parameters.AddWithValue("@Ushortname", txtUserShortName.Text);
cmd.Parameters.AddWithValue("@Pswd", txtPassword.Text);
cmd.Parameters.AddWithValue("@UCODE", LblUcode.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
没有错误,数据根本没有更新。
答案 0 :(得分:0)
试试这个: -
using(SqlConnection conn = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("update users Set uname=@Uname,ushortname=@Ushortname,pswd=HASHBYTES('MD5',@Pswd) where ucode=@UCODE ", conn))
{
cmd.Parameters.AddWithValue("@Uname", txtUserName.Text);
cmd.Parameters.AddWithValue("@Ushortname", txtUserShortName.Text);
cmd.Parameters.AddWithValue("@Pswd", txtPassword.Text);
cmd.Parameters.AddWithValue("@UCODE", UCODE);
conn.Open();
cmd.ExecuteNonQuery();
}
}
您在Ucode中缺少@
。此外,将代码封装在using
块检查this中是一种很好的做法。
答案 1 :(得分:0)
参见下面编辑的代码: -
if (!PostBack)
{
string databaseconnectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
string sql = "update users Set uname=@Uname,ushortname=@Ushortname,pswd=HASHBYTES('MD5',@Pswd) where ucode=@UCODE";
using (SqlConnection conn = new SqlConnection(databaseconnectionstring))
{
conn.Open();
using (SqlCommand cmd= new SqlCommand())
{
cmd.CommandText=sql;
cmd.Connection=databaseconnectionstring;
cmd.Parameters.AddWithValue("@Uname",txtUserName.Text );
cmd.Parameters.AddWithValue("@Ushortname",txtUserShortName.Text );
cmd.Parameters.AddWithValue("@Pswd",txtPassword.Text );
cmd.Parameters.AddWithValue("@UCODE", UCODE);
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
}
}
}
答案 2 :(得分:0)
您需要确保参数名称与它们在sql字符串中的作用完全匹配。
cmd.Parameters.AddWithValue("@UCODE", UCODE);
此外,要么将所有内容包装在using或try / finally中以处理连接。我假设你已经有代码在尝试执行之前打开连接。
答案 3 :(得分:0)
protected void Page_Load(object sender, EventArgs e)
{
string bsname = Request.QueryString["bsname"];
string bscode = Request.QueryString["bscode"];
if (!string.IsNullOrEmpty(bsname))
{
if (string.IsNullOrEmpty(txtBsName.Text))
{
SqlCommand cmd = new SqlCommand("select * from bs WHERE bscode=@bscode", conn);
SqlDataAdapter dadapter = new SqlDataAdapter();
conn.Open();
txtBsName.ReadOnly = false;
dadapter.SelectCommand = cmd;
cmd.Parameters.Add(new SqlParameter("@bscode", bscode));
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
lblBsCode.Text = dr["bscode"].ToString();
txtBsName.Text = dr["bsname"].ToString();
}
dr.Close();
conn.Close();
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
conn.Open();
string UCODE = LblUcode.Text;
SqlCommand cmd = new SqlCommand("update users Set uname=@Uname,ushortname=@Ushortname,pswd=HASHBYTES('MD5',@Pswd) where ucode=@UCODE", conn);
cmd.Parameters.AddWithValue("@Uname",txtUserName.Text );
cmd.Parameters.AddWithValue("@Ushortname",txtUserShortName.Text );
cmd.Parameters.AddWithValue("@Pswd",txtPassword.Text );
cmd.Parameters.AddWithValue("@UCODE", UCODE);
cmd.ExecuteNonQuery();
cmd.Dispose();
ShowMessage("Company Data update Successfully......!");
clear();
}
感谢您在上述代码的帮助下完成所有工作。