Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using osql utility

Posted on 2014-03-22
5
Medium Priority
?
328 Views
Last Modified: 2014-03-24
I am trying to create a console application that will do several things like stop a service.  One of the tasks it must do is drop a database.  The code I have come up with is:

string sqlcmd = "sqlcmd –d master –Q drop database test";
                ProcessStartInfo ProcessProperties = new ProcessStartInfo();
                ProcessProperties.FileName = "cmd.exe";
                ProcessProperties.Arguments = sqlcmd;
                ProcessProperties.UseShellExecute = false;
                Process myprocess = Process.Start(ProcessProperties);

When I run the sqlcmd from the commandline then it deletes the database.  



What I am after is passing this to the commandline from my application

sqlcmd -d master -Q "drop database test"  

Can someone please help me with that.  

Thanks.
0
Comment
Question by:sherbug1015
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39948855
There are different ways to stop a service:

1) SC command from a command line
2) ServiceProcess/Controller
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 39949216
I don't see the need for using cmd.exe here. Calling sqlcmd should be enough. You might have to provide the path to it, if PATH isn't set accordingly.
string sqlcmd = "–d master –Q drop database test";
                ProcessStartInfo ProcessProperties = new ProcessStartInfo();
                ProcessProperties.FileName = "sqlcmd.exe";
                ProcessProperties.Arguments = sqlcmd;
                ProcessProperties.UseShellExecute = false;
                Process myprocess = Process.Start(ProcessProperties);

Open in new window

0
 
LVL 80

Expert Comment

by:arnold
ID: 39949370
What is the console application written in? Why not use the direct connection to the server. Then use the alter databse state to single user mode which will disconnect all other sessions which is presumably why you are looking to stop the service.

Other experts may provide an alternative approach if you could provide additional details on what the purpose of what you want to do from A to Z.
0
 

Author Closing Comment

by:sherbug1015
ID: 39950181
Thank you for your time.   I really appreciate it.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 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