在C#中使用SQLCommand批量更新/插入 [英] Batch Update/insert in using SQLCommand in C#

查看:217
本文介绍了在C#中使用SQLCommand批量更新/插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 SQLCommand 实现批处理 update/insert .我想用10个 SQLParameter

How I could achieve batch update/insert using SQLCommand. I wanted to create SQLCommand text dynamically in for loop of MyObject[] in C# with 10 SQLParameter

在批量插入 insert 的情况下,我需要检查每个记录是否已经存在.即

in case of bulk insert, i need to check for every record that it already exist or not. i.e.

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

开始

insert....

结束

这是通过C#完成的.db中没有存储过程

This is to be done from C#.No stored procedure in db

推荐答案

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#中使用SQLCommand批量更新/插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆