gpt4 book ai didi

c# - 在 C# 中使用 SQLCommand 批量更新/插入

转载 作者:太空狗 更新时间:2023-10-29 18:03:37 30 4
gpt4 key购买 nike

我如何使用 SQLCommand 实现批处理 更新/插入。我想在 C# 中 MyObject[]for 循环中动态创建 SQLCommand 文本,其中包含 10 个 SQLParameter

如果是批量插入,我需要检查每条记录是否已经存在。即

如果不存在(从表 1 中选择 pkid,其中 fkid1=@fkid1 和 fkid2=@fkid1)

开始

insert....

结束

这将在 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();

关于c# - 在 C# 中使用 SQLCommand 批量更新/插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6409139/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com