Solved

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

Posted on 2010-11-11
12
933 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 34

Expert Comment

by:Paul MacDonald
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 26

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

27 Experts available now in Live!

Get 1:1 Help Now