Skip to content
Advertisement

Set Sql to Single User mode From C# and Create Admin User

I have a c# winform application which connects to a database and read data. This database is used by another app also. I just executed a query from my app to read some data.

I don’t have the user and password of the database.

So I have to change sql service to single user mode manually : add -m parameter to sql service and then restart the service and Because I have local admin password. I can login with windows login and create user and set sysadmin roll to it.

Now I want to do all this programmability on c#

Is there a way to have Create sysadmin user on sql with windows local admin password with sql command ?

Advertisement

Answer

I was able to do it

first create batch file example test.bat and set code into

first stop sql server

and start -m parameter for start single user mode

“SQLCMD” only connect from command line in single user mode

after start single user mode execute query.sql

-s . server address here my local

-e connect windows authentication

-i sql query for execute

query.sql:

in sql query first check if newuser exist drop then create with sysadmin role

after execute sql query stop sql server and again start normaly without parameter

now execute batch file run as administrator on with c#

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement