C#。用DataAdapter和OleDbCommandBuilder更新数据库提示“重复的索引,主键,或关系值” 20
voidinsertFG(OleDbConnectionconn,DataTabledto,DataTabledtp){conn.Open();OleDbTransact...
void insertFG(OleDbConnection conn, DataTable dto, DataTable dtp)
{
conn.Open();
OleDbTransaction transaction = conn.BeginTransaction();
OleDbCommand cmd = conn.CreateCommand();
cmd.Transaction = transaction;
cmd.Connection = conn;
OleDbDataAdapter oda = new OleDbDataAdapter();
try
{
cmd.CommandText = "select * from hsc_vchr";
oda.SelectCommand = cmd;
OleDbCommandBuilder ocb = new OleDbCommandBuilder(oda);
oda.Fill(dto);
oda.Update(dto);
cmd.CommandText = "select * from hsc_part";
oda.SelectCommand = cmd;
ocb = new OleDbCommandBuilder(oda);
oda.Fill(dtp);
oda.Update(dtp);
transaction.Commit();
MessageBox.Show("保存成功", "-_-");
}
catch (Exception ex)
{
transaction.Rollback();
MessageBox.Show(ex.Message.ToString());
}
conn.Close();
}
执行到oda.Update(dto);报错
我是把整张表调出来,修改后在保存回去,肯定会有重复的值啊。
它把我改完的行,当作新加的行,追加进去 展开
{
conn.Open();
OleDbTransaction transaction = conn.BeginTransaction();
OleDbCommand cmd = conn.CreateCommand();
cmd.Transaction = transaction;
cmd.Connection = conn;
OleDbDataAdapter oda = new OleDbDataAdapter();
try
{
cmd.CommandText = "select * from hsc_vchr";
oda.SelectCommand = cmd;
OleDbCommandBuilder ocb = new OleDbCommandBuilder(oda);
oda.Fill(dto);
oda.Update(dto);
cmd.CommandText = "select * from hsc_part";
oda.SelectCommand = cmd;
ocb = new OleDbCommandBuilder(oda);
oda.Fill(dtp);
oda.Update(dtp);
transaction.Commit();
MessageBox.Show("保存成功", "-_-");
}
catch (Exception ex)
{
transaction.Rollback();
MessageBox.Show(ex.Message.ToString());
}
conn.Close();
}
执行到oda.Update(dto);报错
我是把整张表调出来,修改后在保存回去,肯定会有重复的值啊。
它把我改完的行,当作新加的行,追加进去 展开
2个回答
展开全部
cmd.CommandText = "select * from hsc_vchr";
oda.SelectCommand = cmd;
OleDbCommandBuilder ocb = new OleDbCommandBuilder(oda);
oda.Fill(dto);
oda.Update(dto);
cmd.CommandText = "select * from hsc_part";
oda.SelectCommand = cmd;
ocb = new OleDbCommandBuilder(oda);
oda.Fill(dtp);
oda.Update(dtp);
这两个重复了。应属于不同的cmd
oda.SelectCommand = cmd;
OleDbCommandBuilder ocb = new OleDbCommandBuilder(oda);
oda.Fill(dto);
oda.Update(dto);
cmd.CommandText = "select * from hsc_part";
oda.SelectCommand = cmd;
ocb = new OleDbCommandBuilder(oda);
oda.Fill(dtp);
oda.Update(dtp);
这两个重复了。应属于不同的cmd
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询