如何在本地应用程序中激活Sql-Server服务

时间:2016-11-23 14:48:22

标签: c# sql-server wpf entity-framework

我在典型的WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred'); DBMS_OUTPUT.PUT_LINE('Error code. ' || SQLCODE); DBMS_OUTPUT.PUT_LINE('Error message. ' || SQLERRM); 应用程序中使用本地数据库。我试图运行它进行调试,但它没有工作,并给了我以下错误:

  

    < 2016-11-23T17:59:19.1888435 + 03:30     底层提供程序在Open上失败。     在System.Data.Entity.Core.EntityClient.EntityConnection.Open()      在System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)      在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func 1 func,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)      在System.Data.Entity.Core.Objects.ObjectContext。<> c__DisplayClass47 1.< ExecuteFunction> b__45()      在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func 1操作)      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)      在FirstAttemptForFillingOutTheContent.EngineEntities.SignIn(String mail,String password)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)在c:\ Users \ Media Depp \ Documents \ Visual Studio 2013 \ NewProjects \ SeyyedAmir \ FirstAttemptForFillingOutTheContent \ FirstAttemptForFillingOutTheContent \ DataAccess.cs:line 140     的EntityFramework     建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)     


搜索后我发现我WPF的数据库服务没有激活:
Sql Service
我手动激活它,程序运行正常。如果服务未激活,我如何在OS代码中激活服务。我查看了this这样的帖子,但我需要一些C#代码来执行此操作。

1 个答案:

答案 0 :(得分:1)

您可以使用命令行提示符并使用以下内容:

NET START MSSQLSERVER将SQL Server作为服务启动。

NET START MSSQL$instancename将SQL Server作为服务启动,其中instancename是数据库服务器实例的实际名称。

C#中的两种方式:

var sc = new System.ServiceProcess.ServiceController("MyService", "MyRemoteMachine");
sc.Start();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);

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

使用sc <server> query | find "SERVICE_NAME"

获取服务名称列表。

选项<server>的格式为\\ServerName

实施例

sc \\MyServer stop schedule将停止调度程序服务。