Solved

Using osql utility

Posted on 2014-03-22
5
301 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 68

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 76

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Server 2008 Cluster Fail-over Errors 5 49
Image(2) 3 28
Sql query for filter 12 23
Slow SQL query 12 27
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now