media media - 1 year ago 100
C# Question

How Sql-Server service can be activated in local application

I am writing a

application using a local database. I tried to run that for debugging but it did not work and gave me the following error:

The underlying provider failed on Open.
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass47 1.<ExecuteFunction>b__45()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, ExecutionOptions executionOptions, ObjectParameter[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, MergeOption mergeOption, ObjectParameter[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, ObjectParameter[] parameters)
at FirstAttemptForFillingOutTheContent.EngineEntities.SignIn(String mail, String password) in c:\Users\Media Depp\Documents\Visual Studio 2013\NewProjects\SeyyedAmir\FirstAttemptForFillingOutTheContent\FirstAttemptForFillingOutTheContent\EngineModel.Context.cs:line 61
at FirstAttemptForFillingOutTheContent.DataAccess.SearchUSerDA(String name, String pass) in c:\Users\Media Depp\Documents\Visual Studio 2013\NewProjects\SeyyedAmir\FirstAttemptForFillingOutTheContent\FirstAttemptForFillingOutTheContent\DataAccess.cs:line 140
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

After searching I figured out that the database service of my
is not active:

Sql Service

I activate it and everything is fine now. But my problem is that this application is going to be a commercial one and I need to activate the service in
code itself if the service was not activated. How should I activate it? I searched on net but did not get anything valuable. I have to say that I have looked posts like this but I need some
code to do that or any equivalants that my program can do in the users computers. Thanks in advance.

Answer Source

You can use a command line prompt and use the following:

NET START MSSQLSERVER Starts SQL Server as a service.


NET START MSSQL$instancename Starts SQL Server as a service, where instancename is the actual name of the database server instance.

Two ways in C#:

var sc = new System.ServiceProcess.ServiceController("MyService", "MyRemoteMachine");


 sc <server> start [service name]
 sc <server> stop [service name]

Use sc <server> query | find "SERVICE_NAME"

to get a list of service names.

The option <server> has the form \\ServerName


sc \\MyServer stop schedule will stop the Scheduler service.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download