Solved

asp.net - Any way to execute multiple queries on a single connection?

Posted on 2010-11-11
12
932 Views
Last Modified: 2012-05-10
I know I can use a SQL stored procedure, but...
Is there a way to execute multiple queries on a single connection in my codebehind?


cnn = New SqlConnection(ConnString)

cmd = NewSqlCommand("IF OBJECT_ID('tbl_temp','U') IS NOT NULL" & vbCrLf _
       & "DROP TABLE tbl_temp" & vbCrLf _
       & "GO" & vbCrLf _
       & "SELECT *" & vbCrLf _
       & "INTO tbl_temp" & vbCrLf _
       & "FROM fnc_temp()" & vbCrLf _
       & "GO", cnn)
                           

Thank you.
0
Comment
Question by:Rick
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 33

Expert Comment

by:paulmacd
ID: 34113107
I can't think of any reason that wouldn't work.  I don't think you don't need all the carriage return/line feeds.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 167 total points
ID: 34113224
Well that specific query won't work because GO is only recognised by Management Studio and isn't actually a language keyword.
0
 
LVL 25

Assisted Solution

by:Shaun Kline
Shaun Kline earned 166 total points
ID: 34113231
You can use the semi-colon as the statement terminator:

IF OBJECT_ID('tbl_temp','U') IS NOT NULL DROP TABLE tbl_temp;
GO;
SELECT * INTO tbl_temp FROM fnc_temp();

It's not pretty but works.

Reference: http://stackoverflow.com/questions/710683/when-should-i-use-semicolons-in-sql-server
0
 
LVL 13

Author Comment

by:Rick
ID: 34113258
I tried executing without the line feeds, but it was giving me a " Syntax error near 'GO' ".

So I put a break point right before command execution and copied the command text into SQL Management Studio and ran it.
(Because it didn't have line feeds in code-behind, the command text was a long, single line)
It gave me the same error, " Syntax error near 'GO' ".

When I put line feeds in my code-behind and ran it, I got the error message.
I put a break point before command execution and copied the command text into SQL Management Studio and it ran fine.


0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34113297
Read Carl's message -- the GO statement isn't a real SQL command, just something recognized by SQL Management studio, you cannot use it with the  SQLCommand object.

Just use semicolons between each SQL statement and it should work fine, I use similar multiple-statement commands like this all the time.
cmd = NewSqlCommand("IF OBJECT_ID('tbl_temp','U') IS NOT NULL" & vbCrLf _
       & "DROP TABLE tbl_temp" & vbCrLf _
       & "SELECT *" & vbCrLf _
       & "INTO tbl_temp" & vbCrLf _
       & "FROM fnc_temp()" & vbCrLf _
       & , cnn)

Open in new window

0
 
LVL 13

Author Comment

by:Rick
ID: 34113490
This works, but the problem is that one of my statements is creating a view and another one is creating a function. So when I remove the GOs from my command string and end each statement with a semicolon, I get:

'CREATE FUNCTION' must be the first statement in a query batch.
'CREATE VIEW' must be the first statement in a query batch.

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Assisted Solution

by:mortimer452
mortimer452 earned 167 total points
ID: 34113723
Ahh good point.  There is no batch separator equivalent (that I know of) that you can use in straight T-SQL such as you are describing, you're just going to have to follow the MSSQL rules.  I think you're going to have to just run these as separate commands.

cmd=new SQLCommand("CREATE VIEW bla bla bla", cnn)
cmd.ExecuteNonQuery()
cmd = new SQLCommand("CREATE FUNCTION bla bla bla", cnn)
cmd.ExecuteNonQuery()

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34113765
You should be able to wrap them all up in a transaction an execute it as a single batch too.
0
 
LVL 13

Author Closing Comment

by:Rick
ID: 34113771
That's fine... I just wondered that could be done.

Thanks guys.
0
 
LVL 13

Author Comment

by:Rick
ID: 34113814
How Carl?

0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34113880
I don't think it will work to wrap everything up in one transactino, but this *might* work.  You could wrap each statement with a BEGIN TRAN / COMMIT TRAN statements to create a new "batch."  

This is sortof a hack though and creates additional overhead on the server for tracking these transactions when it's not really necessary to do so.  There's really no performance advantage to sending these commands as a single command rather than two separate ones, unless you have a really slow network connection and don't want to wait on the additional network IO for connecting a second time.

cmd=new SQLCommand("BEGIN TRAN; CREATE VIEW bla bla bla;COMMIT TRAN;BEGIN TRAN;CREATE FUNCTION bla bla;COMMIT TRAN;", cnn)
cmd.ExecuteNonQuery()


0
 
LVL 13

Author Comment

by:Rick
ID: 34114018
Very good point... thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.NET e-commerce website 4 27
Form design in vb.net 7 18
index Out OF Range Exception error 4 27
Adjust the position 3 7
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now