如何发送多个命令到SQL Server 2008的一个往返多个、命令、SQL、Server

2023-09-06 18:30:51 作者:大赢家

我想在一个往返发送多个参数化更新,插入和删除命令到SQL Server数据库。我看着SqlBulkCopy的但似乎有一个映射/表/语句在同一时间只工作。我所期待的一些东西更灵活。我不需要得到任何结果,从任何一个查询(即:受影响的行数),但我不希望失败被传播回应用程序层,这样的交易可以被回滚。任何想法?

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?

侧的问题:我也想知道NHibernate的是怎么做的,不仅是因为它是尝试和真实,似乎表现良好。我试图寻找通过NHibernate的源代码,但尚不清楚它是怎么做配料。我可以回去的源代码,但希望有人知道,高层次,它是如何被处理,并可以解释给我听。

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.

推荐答案

设置的CommandType 文本,独立你的命令用分号。你结束了一个巨大的命令,其参数的名字,你可能需要生成编程(避免冲突)。

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).