Ahmed Abdul Moniem


How to change the SQL Server Express or SQL Server Agent service account programatically using C# 2.0


actually, I do know all the other methods like using SQL Server Configuration Manager in SQL Server 2005 or Manage My Computer dialoge. But I really need to do this using C# 2.0.

Why I need this
I want to do this as a part of an installation procedure to make the user able to backup his database anywhere with any priveleges. And I dont wanna him to do this manually as he is not an expert at all or even a novice.

Can any one help on that

Thanks in advance




Re: SQL Server or SQL Server Agent service account ?!

Jens K. Suessmeyer


See the Service Account changer on my website to see how this is done using the SCM.

Jens K. Suessmeyer

---
http://www.sqlserver2005.de
---







Re: SQL Server or SQL Server Agent service account ?!

Ahmed Abdul Moniem

Thank you so much for that tool
but i wanna really some code can u help me on that






Re: SQL Server or SQL Server Agent service account ?!

Ahmed Abdul Moniem

I tried to use your tool but it gives an InvalidOperationException ..
Can you provide me with a command line makes your tool works fine with me thx so much





Re: SQL Server or SQL Server Agent service account ?!

Ahmed Abdul Moniem

Code Block

private void ChangeSqlExpressServiceAccount()
{
ManagedComputer managedComputer = new ManagedComputer(Environment.MachineName);
installerEventLog.WriteEntry("Service Account is: " + managedComputer.Services["MSSQL$SQLEXPRESS"].ServiceAccount);
managedComputer.Services["MSSQL$SQLEXPRESS"].SetServiceAccount("LocalSystem", "");
sqlServiceController.Stop();
sqlServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, new TimeSpan(0, 2, 0));
sqlServiceController.Start();
sqlServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, 2, 0));
installerEventLog.WriteEntry("Service Account is: " + managedComputer.Services["MSSQL$SQLEXPRESS"].ServiceAccount);
}


Note:
installerEventLog = EventLog Control
sqlServiceController = ServiceController Control

this function changes the account of the service to the local system ..

no need for any tool ..





Re: SQL Server or SQL Server Agent service account ?!

Andrea Montanari

hi,

just a side note.. using SQL Server Configuration Manager, as Jens idicated, not only sets the account the service has to run as, but performs additionals steps, like granting the required permissions at NTFS level for the account to be used, required as indicated here..

regards




Re: SQL Server or SQL Server Agent service account ?!

Ahmed Abdul Moniem

if you refer to this
http://msdn2.microsoft.com/en-us/library/ms180499.aspx

you will find the following:

WMI Provider for Configuration Management

The WMI provider is a published layer that is used with the SQL Server Configuration Manager snap-in for Microsoft Management Console (MMC) and the Microsoft SQL Server Configuration Manager. It provides a unified way for interfacing with the API calls that manage the registry operations requested by SQL Server Configuration Manager and provides enhanced control and manipulation over the selected SQL Server services.



which means as i understood, that this API will make just as the SCM will do!