首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

textbox绑定并更新sql数据库,该怎么处理

2012-04-19 
textbox绑定并更新sql数据库我的代码如下:string sql update UserTable set UserID + txtID.Text.To

textbox绑定并更新sql数据库
我的代码如下:  
 string sql = "update UserTable set UserID='" + txtID.Text.ToString() + "',UserName='" + txtName.Text + "',USex='" + txtSex.Text + "',UBirthday='" + txtBIr.Text.ToString() + "',DepartID='" + txtDepa.Text.ToString() + "',photo='"+phototext+"'";
   
SqlConnection con = new SqlConnection(DBHelper.constr);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "GoodTable");
SqlCommandBuilder builder = new SqlCommandBuilder(da);
con.Close();
DBHelper.ExecuteNonQuery(sql)
提示不能重复插入,usertable设置了主键
应该怎么解决。才能更新

[解决办法]
他们说的都没说到关键上,你没发现你的sql没设条件吗,这样是全部更新啊亲,如果userID是主键,就这么写:string sql = "update UserTable set UserName='" + txtName.Text + "',USex='" + txtSex.Text + "',UBirthday='" + txtBIr.Text.ToString() + "',DepartID='" + txtDepa.Text.ToString() + "',photo='"+phototext+"' where UserID='" + txtID.Text.ToString() + "'"; 你这样写会把所有的改成一样的记录,然后有主键,当然会提示这个了。

[解决办法]
你的sql语句Upeate更新怎么没有where条件呢?
[解决办法]
你这句话如果顺利执行会更新所有的行,加个where 先吧

热点排行