在 C# 中使用 SQLCommand 进行批量更新/插入

2023-11-23

我怎样才能实现批量update/insert using SQLCommand。我想创造SQLCommand动态文本for的循环MyObject[]在 C# 中为 10SQLParameter

如果是散装的insert,我需要检查每条记录是否已经存在。 IE。

if not Exists(select pkid from table1 where fkid1=@fkid1 and fkid2=@fkid1)

begin

insert....

end

这是从 C# 完成的。数据库中没有存储过程


SqlCommand command = new SqlCommand();
// Set connection, etc.
for(int i=0; i< items.length; i++) {
    command.CommandText += string.Format("update mytable set s_id=@s_id{0} where id = @id{0};", i);
    command.Parameters.Add("@s_id" + i, items[i].SId);
    command.Parameters.Add("@id" + i, items[i].Id);
}
command.ExecuteNonQuery();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C# 中使用 SQLCommand 进行批量更新/插入 的相关文章

随机推荐