Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using osql utility

Posted on 2014-03-22
5
Medium Priority
?
320 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 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 79

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

719 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