March 28, 2011 at 2:44 pm
Hello Experts,
I am working on a database driven application using C#.Net and VS 20055. I am
using Sq Server 2005 Express as my database engine. This is production application and not just a student project. I have created a setup project as well as a installer class within which I am performing post-install actions. I have marked Sql Server 2005 as a prerequisite which means the setup will try to install it if it does not exist on the target machine. In my installer class i am restoring the database programmatically using SMO. But I need to perform some configuration change to the Sql Server such as enabling remote connection and mixed authentication mode before restoring the database in order to prevent restoration failed. Also enabling remote connection is necessary as the application uses Tcp/Ip to connect to Sql Server.
Please tell me if there is a way to change such configurations programmatically.
Many Thanks.
March 30, 2011 at 3:04 pm
Hi All,
It seems none of experts here know how to accomplish this task. But I would announce that I have found the solution and it works fine. So I would like to share it with you.
Actually configure an instance using Wmi in the Smo assembly. ManagedComputer class in Wmi lets you gain access to a specific instance and configure using the instance's properties which you can access through ServerInstances["InstanceName"]; Once you have changed configuration values you must call Alter() method of the instance class you just obtained through ServerInstances collection.
Additionally you need to restart the service in order the changes to affect the instance. For this you can use the following code:
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.MachineName = ".";
sc.ServiceName = "MSSQL$SQLEXPRESS"; // (You must prefix the instance name with MSSQL$).
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
It is also a good idea to your application rest (sleep) for a second to ensure the Sql Server instance is completely initialized. For this you use the following code:
Thread.Sleep(1000);
I hope this information helps other developers.
Best Regards.
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply