.NET foreach内判断插入重复数据

yunleizhilian 2009-03-09 02:27:31
SqlDataAdapter mda = new SqlDataAdapter("select * from DEMO", conn);
ds.Clear();
mda.Fill(ds, "insert");
foreach (DataRow dr in ds.Tables["insert"].Rows)
{
SqlConnection con = new SqlConnection(stringSql);
cmd1.CommandText = "insert into SI_INFO(SI_ID,SI_NO) values('"+cutid+"' ,'" + dr["DEMO_ZD"].ToString() + "')";
cmd1.ExecuteNonQuery();
}

-------------------------------
我从DEMO表里把cutid和DEMO_ZD 2个字段插入到SI_INFO表里..

我想问下每次插入数据的时候先判断下SI_NO是否在SI_INFO表内存在.存在者跳过.插入下一条...避免重复...

DEMO表内没主键.有DEMO_ZD表示号码的意思...

谢谢各位大哥了..
...全文
151 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
relive_qiankai 2009-03-09
  • 打赏
  • 举报
回复
楼上正确啊.
yunleizhilian 2009-03-09
  • 打赏
  • 举报
回复
4楼朋友..每次我从DEMO表里去的数据都是最新的...

只是往我的数据库里插入的时候需要判断..最新的数据是否在我的库里已存在了!


所以在DEMO里去 去重是没什么用的...
yunleizhilian 2009-03-09
  • 打赏
  • 举报
回复
Connection我写到循环外面就可以了 这个不是问题...请问怎么在循环内去判断
zhaozhijun0207 2009-03-09
  • 打赏
  • 举报
回复

select distinct * from DEMO
yunleizhilian 2009-03-09
  • 打赏
  • 举报
回复
2楼的意思我都明白,我想问下怎么实现...1楼大哥能写的仔细点么???谢谢了饿
wuyq11 2009-03-09
  • 打赏
  • 举报
回复
先查询数据是否存在,没有再添加
北京的雾霾天 2009-03-09
  • 打赏
  • 举报
回复
存在则跳过,在Foreac里可以直接使用Continue就行了。

不过你在Foreach里的每一个循环都New一个Connection不太合适,你可以这样改一下:

using SqlConnection con = new SqlConnection(stringSql))
{
foreach (DataRow dr in ds.Tables["insert"].Rows)
{
...
}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧