Solved

Using osql utility

Posted on 2014-03-22
5
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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 78

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 shrink a transaction log file down to a reasonable size.

752 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