I'm trying to create something similar to SQL Server Management Studio.
I want the ability to run several queries on the same connection like one does in the same tab in SSMS, however from c# each SqlCommand
is executed separately even with the same SqlConnection
so they can't see each others DECLARE
's
Example: If you run two queries:
DECLARE @ted varchar(100) = 'Ted A';
SELECT @ted as [Query1];
and the other
SELECT @ted as [Query2];
If you run these two, in order, separately in a SSMS
tab, you should get two correct results, one from each query.
However the problem I have is if I have one SqlConnection
and I create two SqlCommand
objects then they don't see the @ted
variable even though they are the same connection.
How can I create a connection to a sql server database so that it behaves like an SSMS tab in that each subsequent query is in the same scope, and therefore I can use @variables
in all queries?
Note: the same behavior as in SSMS can also be found in the SQLCMD
utility.
Edit: i have just realised what i have asked is not how SSMS works, this is actually impossible. you should be able to access #tables etc from the same connection but not @variables