如何使用ado.net进行多次插入 [英] How to do multiple inserts with ado.net

查看:62
本文介绍了如何使用ado.net进行多次插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用cmd对象来执行带参数的存储过程,但我是否必须一遍又一遍地执行此操作?如果我想执行如下所示的sql:


插入UserCompanyRoles(CompanyId,UserId,RoleID)

I know how to use a cmd object to execute a stored procedure with parameters, but do i have to do that over and over?  If i wanted to execute sql like below:

insert into UserCompanyRoles(CompanyId, UserId, RoleID)

值(4, 1,1)

插入UserCompanyRoles(CompanyId,UserId,RoleID)
< span style ="color:#008000; font-size:x-small">值(4,1,2)

insert into UserCompanyRoles(CompanyId, UserId, RoleID)
Values(4,1,2)

插入UserCompanyRoles(CompanyId,UserId,RoleID)
值(4,1,3)

insert into UserCompanyRoles(CompanyId, UserId, RoleID)
Values(4,1,3)

插入UserCompanyRoles(CompanyId,UserId,RoleID)
值(4,1,4)

insert into UserCompanyRoles(CompanyId, UserId, RoleID)
Values(4,1,4)

推荐答案

这取决于您需要实现的目标以及您正在使用的数据库类型。如果它是SQL Server,您可以收集所有项目,将它们格式化为某种XML结构,并将该XML字符串发送到存储过程,在那里它可以作为单个批处理而不循环。如果你不能一次发送项目,那么你需要逐个循环并从客户端应用程序调用INSERT SQL语句。
It depends on what you need to achieve and what kind of database you are using. If it's SQL Server, you could collect all the items, format them as some sort of XML structure and sent that XML string to stored procedure where it could be handled as single batch without looping. If you cannot send items in one shot, then you would need to loop and call INSERT SQL statement from the client application one-by-one.


这篇关于如何使用ado.net进行多次插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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