Solved

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

Posted on 2010-11-11
12
939 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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