我用DATATABLE做容器存放数据用SqlDataAdapter.Update更新 DATATABLE 修改过的数据。总提示我‘违反了 PRIMARY KEY 约束 不能在对象 中插入重复键’
//定义
public int UpdateData(string strCommand, DataTable dt)
{
try
{
int num = 0;
SqlDataAdapter adapter = new SqlDataAdapter(strCommand, new SqlConnection(this.DB_String));
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
num += adapter.Update(dt);
dt.AcceptChanges();
return num;
}
catch (Exception)
{
return -1;
}
}
//调用
DataTable dt=new DataTable();
//查询得到DT里的内容
//修改DT里的内容
this.UpdateData("select * from t1",dt);
//定义
public int UpdateData(string strCommand, DataTable dt)
{
try
{
int num = 0;
SqlDataAdapter adapter = new SqlDataAdapter(strCommand, new SqlConnection(this.DB_String));
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
num += adapter.Update(dt);
dt.AcceptChanges();
return num;
}
catch (Exception)
{
return -1;
}
}
//调用
DataTable dt=new DataTable();
//查询得到DT里的内容
//修改DT里的内容
this.UpdateData("select * from t1",dt);
解决方案 »
- 要从华为T2000网管获取告警信息,但找不到corba北向接口资料。有人能提供点吗?
- Out of memory 凭什么!?
- 求一个数组算法,不,是解法,只要能解就谢谢了。(80分,在线等~)
- 如何用C# 在Winform 中从 Excel导数据到 girdview显示,急,谢谢
- 急!!!在程序中动态生成的dialog如何取到它上面的textbox的值?
- 找c#的类库文档■C#类库电子文档■
- 高手指点短信猫问题!向串口写了AT命令,为什么串口没响应,读到的东西是发出去的命令,已经考虑了延时!
- 在c#中调用word时怎么样调用自己做的word模板,就像调用excel模板一样
- 大虾帮忙灌水:在定义类时,插入"[...]"是什么意思?
- 今天运行windform,发现一个Dictionary的bug
- C# 安装部署项目的卸载程序出现的问题?
- 为什么我的进度条显示不起作用
--操作数据库---
adapter.Update(dt); //更新ds到数据库