执行多个查询
我正在使用 OleDB 在 C# 中执行查询,
有什么方法可以在一个命令语句中执行多个查询吗?
我尝试用分号 (;) 将它们分开,但它给出错误“在末尾找到字符”
我必须一次执行数百个查询。
编辑:我正在执行插入语句。
I am using OleDB for executing my queries in C#,
Is there any way I can execute multiple queries in one command statement?
I tried to separate them with semi-colon (;) but it gives error "Characters found at the end"
I have to execute a few hundreds of queries at once.
Edit: I am executing Insert Statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无法在一个
OleDbCommand
。如果可能,请创建一个存储过程,否则您将不得不坚持在服务器上触发许多 OleDbCommand。但值得注意的是,连接池已启用 默认情况下
OleDbConnection
:编辑:
尝试这样的操作:
根据您连接到的 OleDb 提供程序,您也许可以使用它。但要注意,无论如何,它可能会像一条一条插入记录一样慢。
It's not possible to combine queries within one
OleDbCommand
. If possible, make a stored procedure, otherwise you'll have to stick to firing many OleDbCommands at the server.It's worth noting, though, that connection pooling is enabled for
OleDbConnection
by default:EDIT:
Try something like this:
Depending on which OleDb provider you're connecting to, you might be able to use this. But beware, it might be as slow as inserting records one by one anyway.
只需使用 GO(对批次进行分组)和冒号来对它们进行批处理即可分隔批次内的查询。确保冒号周围有空格。您需要将此 SQL 提交到 sp_executesql。
示例取自 MSDN。
Simply Batch them up using GO (groups a batch) and colons to separate queries inside a batch. Make sure to surround your colon with spaces. You need to submit this SQL to sp_executesql.
Example taken from MSDN.
使用sp_executesql。
请参阅我在另一个问题中的回答,其中包含使用
sp_executesql
批量发送 SQL 查询的示例。Use
sp_executesql
.Do see my answer in another question where I include a sample usage of
sp_executesql
to send SQL queries in a batch.我想在我正在处理的项目中使用
OleDB
在 Access 数据库中执行多个 SQL 语句,但我找不到任何适合我的情况的东西,所以我想出了这个解决方案它基本上将 SQL 字符串分解为多个 SQL 语句并在一个事务中执行它们:希望它对某人有帮助。
I wanted to execute multiple SQL statements in an Access database using
OleDB
for a project I'm working on, and I couldn't find anything that's good enough for my situation, so I came up with this solution which basically breaks down the SQL string into multiple SQL statements and executes them in one transaction:Hope it helps someone.