473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SqlCommand + T-SQL Statements

I'm trying to run this T-SQL code using SqlCommand:

USE master;
ALTER DATABASE AdventureWorks
SET SINGLE_USER;
GO

I did it like that:

public static int ObtainExclusiveAccess(Server srv, string database)
{
string cmd = @"USE master; ";
cmd += "ALTER DATABASE " + database;
cmd += @" SET SINGLE_USER ";
//cmd += "GO"; //if i add this got SqlException:
// Incorrect syntax near 'GO'

SqlConnection sqlConn =
new SqlConnection(srv.ConnectionContext.ConnectionStri ng);

sqlConn.Open(); //it's opened for sure, i've checked it
SqlCommand cmdExclusiveAccess = new SqlCommand(cmd,sqlConn);

int result = cmdExclusiveAccess.ExecuteNonQuery();
//result is -1
//string response = (string)cmdExclusiveAccess.ExecuteScalar();
//tried to get: Changed database context to 'master'. but was null
sqlConn.Close();

return result;

}

What am I doing wrong, is SqlCommand good way to run this query
ps i'am logging as user "sa" with membership db_owner, so it is not this

Thanks in advance
Mar 23 '06 #1
4 2464
ExecuteNonQuery is not something you can just call on an arbitrary SQL
script. You can't use GO statements.

If you need to run some arbitrary sql statements like you were using query
analyzer, you need to use SQLDMO.

"christof" <no****@nomail.de> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
I'm trying to run this T-SQL code using SqlCommand:

USE master;
ALTER DATABASE AdventureWorks
SET SINGLE_USER;
GO

I did it like that:

public static int ObtainExclusiveAccess(Server srv, string database)
{
string cmd = @"USE master; ";
cmd += "ALTER DATABASE " + database;
cmd += @" SET SINGLE_USER ";
//cmd += "GO"; //if i add this got SqlException:
// Incorrect syntax near 'GO'

SqlConnection sqlConn =
new SqlConnection(srv.ConnectionContext.ConnectionStri ng);

sqlConn.Open(); //it's opened for sure, i've checked it
SqlCommand cmdExclusiveAccess = new SqlCommand(cmd,sqlConn);

int result = cmdExclusiveAccess.ExecuteNonQuery();
//result is -1
//string response = (string)cmdExclusiveAccess.ExecuteScalar();
//tried to get: Changed database context to 'master'. but was null
sqlConn.Close();

return result;

}

What am I doing wrong, is SqlCommand good way to run this query
ps i'am logging as user "sa" with membership db_owner, so it is not this

Thanks in advance

Mar 23 '06 #2
Marina Levit [MVP] wrote:
ExecuteNonQuery is not something you can just call on an arbitrary SQL
script. You can't use GO statements.

If you need to run some arbitrary sql statements like you were using query
analyzer, you need to use SQLDMO.


So I can use Smo (Microsoft.SqlServer.Management.Smo) i understand that,
but is there any possibility do make remote administration with only
queries (simple textbox or textarea and Run button), is there no chance
to run T-SQL queries, but the only solution is using Server, Database...
(Smo) classes? Because in that case each query should be exchanged for
Smo methods properties...

Thanks
Mar 23 '06 #3
I haven't looked at SMO. I used to use SQLDMO and I know that there was an
interface for submitting simple queries. I imagine that this interface is
still in SMO. If you dig around, you may get lucky. Sorry I can't be more
helpful.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"christof" <no****@nomail.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Marina Levit [MVP] wrote:
ExecuteNonQuery is not something you can just call on an arbitrary SQL
script. You can't use GO statements.

If you need to run some arbitrary sql statements like you were using
query analyzer, you need to use SQLDMO.


So I can use Smo (Microsoft.SqlServer.Management.Smo) i understand that,
but is there any possibility do make remote administration with only
queries (simple textbox or textarea and Run button), is there no chance to
run T-SQL queries, but the only solution is using Server, Database...
(Smo) classes? Because in that case each query should be exchanged for Smo
methods properties...

Thanks

Mar 24 '06 #4
If it is just going to be simple queries (no GO statements), then you can
use ADO.NET. If a user enters a query in that has a GO statement though,
ADO.NET is going to throw an exception.

"christof" <no****@nomail.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Marina Levit [MVP] wrote:
ExecuteNonQuery is not something you can just call on an arbitrary SQL
script. You can't use GO statements.

If you need to run some arbitrary sql statements like you were using
query analyzer, you need to use SQLDMO.


So I can use Smo (Microsoft.SqlServer.Management.Smo) i understand that,
but is there any possibility do make remote administration with only
queries (simple textbox or textarea and Run button), is there no chance to
run T-SQL queries, but the only solution is using Server, Database...
(Smo) classes? Because in that case each query should be exchanged for Smo
methods properties...

Thanks

Mar 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
3
by: Eduardo Rosa | last post by:
I can't undertand why that's don't works, can somebody help me? thanks a lot Description: An error occurred during the compilation of a resource required to service this request. Please...
1
by: Tom | last post by:
Hello guys Please have a look on following paragraph from ".NET Data Access Architecture Guide". ''''''''''' Although you can repeatedly use the same SqlCommand object to execute the same...
3
by: Jason Huang | last post by:
Hi, In C#.Net, can we have more than one SqlCommand in a SqlConnection? And in one SqlCommand, can we have more than one SqlDataReader in a SqlCommand? In what situation should I use...
0
by: Agnes | last post by:
I got a silly question about Sqlcommand . In my form, there are 5 methods which use the same sqlcommand to execute reader. I had create and dispose for each sqlcommand in every method. Some said...
10
by: John Bailo | last post by:
I want to pass a SqlCommand object as a input parameter to a method. I want to pass the SqlCommand "by value" so that any updates to the original object are *not* reflected in the object within...
1
by: job | last post by:
how is it possible to serialize/de-serialize a SqlCommand?
0
by: =?Utf-8?B?ZGF2ZQ==?= | last post by:
The following finishes in 1 hour instead of erroring after 1 second as expected. Dim cn As New System.Data.SqlClient.SqlConnection cn.ConnectionString = "Server=(local); Database=master; User...
6
by: Frank Hauptlorenz | last post by:
Hello, I'm trying to send an SqlCommand to a WCF-Service. For this I'm using the following DataContract: public class SqlCommandComposite { SqlCommand cmd = new SqlCommand();
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.