如何通过一次往返向SQL Server 2008发送多个命令

时间:2022-09-25 10:38:31

I want to send multiple parametrized update, insert and delete commands to a sql server database in one round trip. I've looked at sqlbulkcopy but that appears to only work with one mapping/table/statement at a time. I was hoping for some thing a more flexible. I don't need to get any results from any of the queries (ie: affected row count) but I do want failures to be propagated back to the app layer and so that the transaction can be rolled back. Any ideas?

我想在一次往返中向sql server数据库发送多个参数化更新,插入和删除命令。我看过sqlbulkcopy,但似乎一次只能使用一个映射/表/语句。我希望有些东西更灵活。我不需要从任何查询中获得任何结果(即:受影响的行数)但我确实希望将失败传播回应用层,以便可以回滚事务。有任何想法吗?

side question: I was also wondering how nHibernate does it, only because it is tried and true and appears to perform well. I attempted looking through the nhibernate source but it wasn't immediately clear how it was doing the batching. I can go back to the source but was hoping someone knew, high level, how it is being handled and could explain it to me.

旁边的问题:我也想知道nHibernate是如何做到的,只是因为它是经过尝试和真实的并且似乎表现良好。我试图查看nhibernate源代码,但目前还不清楚它是如何进行批处理的。我可以回到源头,但希望有人知道,高级别,如何处理,并可以向我解释。

2 个解决方案

#1


1  

Set CommandType to Text, and separate your commands with semicolons. You end up with one giant command, whose parameter names you may need to generate programmatically (to avoid conflicts).

将CommandType设置为Text,并用分号分隔命令。您最终得到一个巨大的命令,您可能需要以编程方式生成其参数名称(以避免冲突)。

#2


0  

if i am not wrong SqlCommandBuilder will help you :) . SqlCommandBuilder

如果我没错,SqlCommandBuilder会帮助你:)。 SqlCommandBuilder

#1


1  

Set CommandType to Text, and separate your commands with semicolons. You end up with one giant command, whose parameter names you may need to generate programmatically (to avoid conflicts).

将CommandType设置为Text,并用分号分隔命令。您最终得到一个巨大的命令,您可能需要以编程方式生成其参数名称(以避免冲突)。

#2


0  

if i am not wrong SqlCommandBuilder will help you :) . SqlCommandBuilder

如果我没错,SqlCommandBuilder会帮助你:)。 SqlCommandBuilder