4
I wonder if it is possible to stop SQL Server using C lines of code#.
I have a C# application and would like to add a button to stop the SQL service or the instance, so I can work with the files. mdf and . ldf either to making bakup or replacing. I know it is possible to stop manually through the Windows service manager but I want to do this in a practical way.
I tested, but it didn’t work. Should he open the 'CMD' window? I used exactly the code you informed me changing only the name of the instance 'MSSQL$SQLEXPRESSPOS4'. I must make some other changes?
– Ronison Matos
No, he won’t open the CMD because of that line
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

if you want the CMD to appear just comment on it. since it did not work do the test of the commandnet stop
with your instance directly in the CMD and see if it will work.– Lodi
I went straight to CMD and gave "System error 5. Access denied". I ran CMD as administrator and it worked. But even running my application as an administrator and it still doesn’t work. Would have some way my program run CMD as administrator?
– Ronison Matos
process.StartInfo.Verb = "runas";
tries to add this command before.StartInfo = startInfo;
– Lodi
We’re almost there (laughs.). I commented on the line
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
to see exactly what was going on at the CMD. The CMD opens however the command is not typed inside it and is not executed, in the CMD window that opened I typed the codenet stop MSSQL$SQLEXPRESSPOS4
and it worked. What can be?– Ronison Matos
haha missed a
/c
in this linestartInfo.Arguments = "/c net stop MSSQL$SQLEXPRESS";
Add it and test it.– Lodi
It worked!!! Thank you very much!
– Ronison Matos