ADO.NET Command.CommandText 属性中是否可以有多个 SQL 指令?
摘要
我目前正在编写一个应用程序,我已将 SQL 指令放入项目的参数中。
在代码中,我获取查询的值,该值返回查询本身。比方说,我的 SQL 查询如下:
select col1, col2, col3 from my_table
此外,col1
、col2
和 col3
来自不同的表,并作为外键迁移进入 my_table。因此,在插入时,我必须执行多个 INSERT 语句才能从其他表中获取上述列的值。假设如下:
BEGIN TRANSACTION
insert into first_table (col_x, col_y) values ('col_x', 'col_y')
insert into second_table (col_z, col_a) values ('col_z', 'col_a')
insert into third_table (col_b, col_c) values ('col_b', 'col_c')
最后:
insert into my_table (col1, col2, col3, col_v) values (@col1, @col2, @col3, 'col_v')
COMMIT
假设这些 col1
、col2
、col3
列是表 first 的自增整数
、第二
和第三
。
问题
我能否将复杂的 SQL 语句写入 IDbCommand.CommandText 属性,同时每条指令用分号 (
;
) 分隔?是否可以在此
CommandText
属性中包含BEGIN TRANSACTION...COMMIT/ROLLBACK
?简而言之,我可以写这样的东西吗?
使用 cnx = New SqlConnection(connString) 使用 cmd = cnx.CreateCommand() cmd.CommandText = "开始交易" _ & “插入first_table(col_x,col_y)值('col_x','col_y');” _ & “插入第二表(col_z,col_a)值('col_z','col_a');” _ & “插入third_table(col_b,col_c)值('col_b','col_c');” _ & “插入my_table(col1,col2,col3,col_v)值(@col1,@col2,@col3,'col_v');” _ & “犯罪” cmd.ExecuterNonQuery() 结束使用 结束使用
编辑#1
我应该之前提到过...麦克的答案是我想要走的路,但我不能这样做,因为我的客户的 IT 部门内部有奇怪的政策,除非我使用他们的自定义组件,为了简单起见,我宁愿避免使用它们。请注意,无论如何我都赞成麦克的答案,因为无论如何它都是一个可行的解决方案。
预先感谢您宝贵的帮助和时间!这对我来说至关重要!
Summary
I'm currently writing an application where I have located my SQL instructions into a project's parameters.
Within the code, I get the value of my query which returns the query itself. Let's for instance say that my SQL query is like so:
select col1, col2, col3 from my_table
Besides, col1
, col2
and col3
are from different tables and are migrated as foreign key into my_table. So, when it comes to the insert, I have to perform multiple INSERT statements to get the values from the other tables for these above-mentioned columns. Let's say as follows:
BEGIN TRANSACTION
insert into first_table (col_x, col_y) values ('col_x', 'col_y')
insert into second_table (col_z, col_a) values ('col_z', 'col_a')
insert into third_table (col_b, col_c) values ('col_b', 'col_c')
and finally:
insert into my_table (col1, col2, col3, col_v) values (@col1, @col2, @col3, 'col_v')
COMMIT
Take it that these col1
, col2
, col3
columns are auto-increment integers for tables first
, second
and third
.
Questions
Could I write a complex SQL statement into the IDbCommand.CommandText property while each instruction would be separated by a semicolon (
;
)?Is it possible to include a
BEGIN TRANSACTION...COMMIT/ROLLBACK
into thisCommandText
property?In short, could I write something like this?
Using cnx = New SqlConnection(connString) Using cmd = cnx.CreateCommand() cmd.CommandText = "BEGIN TRANSACTION " _ & "insert into first_table (col_x, col_y) values ('col_x', 'col_y');" _ & "insert into second_table (col_z, col_a) values ('col_z', 'col_a');" _ & "insert into third_table (col_b, col_c) values ('col_b', 'col_c');" _ & "insert into my_table (col1, col2, col3, col_v) values (@col1, @col2, @col3, 'col_v'); " _ & "COMMIT" cmd.ExecuterNonQuery() End Using End Using
EDIT #1
I should have mentioned it before... Mack's answer is the way I would like to go, except that I can't because of strange policies within the IT department of my client, except if I use their custom component which I rather avoid for simplicity sake. Notice that I upvoted Mack's answer anyway since it is a viable solution no matter what.
Thanks in advance for your precious help and time! This is crucial for me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不能使用存储过程,那么这段代码可能会满足您的要求:
我已根据需要添加了表变量以使示例代码运行,显然您可以利用永久表。
If you can't use stored procedures then perhaps this code may meet your requirements:
I have added table variables as necessary to get the example code running, obviously you can utilise your permanent tables.
尝试使用交易...
Try using transactions...