Solved

Using osql utility

Posted on 2014-03-22
5
304 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 69

Accepted Solution

by:
Qlemo earned 500 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 77

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

770 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