Sunday, 15 August 2010

c# - Multiple queries on same connection using same T-SQL variables -


I am trying to create something similar to SQL Server Management Studio

I run multiple queries in the same tab in the same tab in the SSMS, though each with SqlCommand with the same SqlConnection Is executed so they can not see, for example: if you run two questions:

  DECLARE @ted varchar (100) = 'Ted A'; Each other  DECLARE  's 

example: Select [query 1] came in the form of; Ended in

and other

  SELECT {query2];  

If you want to run both of these, in a separate SSMS tab, you should get two correct results, one of each query.

Although I have a problem if I have a SqlConnection and I create two SqlCommand objects then they are @ted Does not appear even if they have the same connection.

How can I create a connection to a SQL Server database so that it behaves like an SSMS tab, after which each question is in the same area, and so I @variables all questions?

Note: Similar behavior of SSMS can also be found in the SQLCMD utility.

EDIT: I have realized what I said, it is not how SSMS works, it is really impossible you should be able to use the #tables etc. from the same connection, But do not have @variables

itemprop = "text">

You should think about it in a single query window in SSMS, almost a single SQLCommand Equivalent to and a single example equals approximately one SqlConnection of SSMS. In the TSQL variable, the batch scope is not the scope of the connection

A SQL command performs a batch of TSQL, possibly with many executable statements. You can put both of your questions in a single order.

Now, if you want multiple values ​​from your values ​​for returns , then you will use the NextResult method of SqlDataReader to transfer it to the next result of the next statement group. Process the results of each statement, then use NextResult to interact through the results set.

simple example

  queryText = "declare @tetvarchar (100) = 'ted a' SELECT @ted - or by using some other query @ted; select @ Bet - or yet another query has been used; command = new SQL command (query text); reader = command.ExecuteReader (); Do {// result set handling code goes here {// record level handling code Here reads} while reader. Read (); // more result set handling code goes here} while reader.extResult ();  

hopefully That it helps


No comments:

Post a Comment