Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

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

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
Rick
Asked:
Rick
  • 5
  • 3
  • 2
  • +2
3 Solutions
 
Paul MacDonaldDirector, Information SystemsCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
Well that specific query won't work because GO is only recognised by Management Studio and isn't actually a language keyword.
0
 
Shaun KlineLead Software EngineerCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RickAuthor Commented:
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
 
mortimer452Commented:
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
 
RickAuthor Commented:
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
 
mortimer452Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
You should be able to wrap them all up in a transaction an execute it as a single batch too.
0
 
RickAuthor Commented:
That's fine... I just wondered that could be done.

Thanks guys.
0
 
RickAuthor Commented:
How Carl?

0
 
mortimer452Commented:
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
 
RickAuthor Commented:
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!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now