我用什么字符串连接到SQL Server?

我正在尝试通过自定义活动使用CRM工作流执行SQL作业。 在我的代码中,一个在线教程让我像这样构建它(在c#中):

[CrmWorkflowActivity("A test activity to run SQL Jobs")] public sealed class ExecuteSQLJob : System.Activities.CodeActivity { #region Inputs [Input("Job Name")] [Default("BMS_ExtractTransformLoad")] public InArgument JobName { get; set; } [Input("Server Connection")] [Default("HBSSQL2008/MSSQLSERVER")] //<--This String public InArgument ServerName { get; set; } [Input("User Name")] [Default("-----")] public InArgument UserName { get; set; } [Input("Password")] [Default("-----")] public InArgument Password { get; set; } #endregion protected override void Execute(CodeActivityContext context) { Server server = new Server(ServerName.Get(context)); //<--Is used here try { server.ConnectionContext.LoginSecure = false; server.ConnectionContext.Login = UserName.Get(context); server.ConnectionContext.Password = Password.Get(context); server.ConnectionContext.Connect(); Job job = server.JobServer.Jobs[JobName.Get(context)]; job.Start(); } finally { if (server.ConnectionContext.IsOpen) { server.ConnectionContext.Disconnect(); } } } } 

但是,当我尝试运行此工作流时,它会抛出一个错误,说它无法连接到服务器。 (具体来说,在server.ConnectionContext.Connect()方法调用上抛出错误)。

问题是serverName String吗? 如果是这样,我怎样才能找到要使用的正确serverName?

谢谢你的帮助!

编辑:这是一些额外的信息……

这是我第一次获得服务器和实例名称的方式: 我的SQL查询

这是CRM工作流程中的错误:

 Workflow suspended temporarily due to error: Unhandled Exception: Microsoft.SqlServer.Management.Common.ConnectionFailureException: Failed to connect to server HBSSQL2008. at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect() at ExecuteSQLJob.ExecuteSQLJob.Execute(CodeActivityContext context) at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation) Inner Exception: System.Data.SqlClient.SqlException: Login failed for user 'rnkelch'. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at Microsoft.SqlServer.Management.Common.ConnectionManager.InternalConnect(WindowsIdentity impersonatedIdentity) at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect() 

对不起,我原来没有包含这个,谢谢你的帮助到目前为止

尽管与问题并不完全相关,但仍然是一个有用的网站 ,可以帮助您正确获取连接字符串。

对于SQL Server 2008,请在同一站点上看到此链接

服务器字符串的部分是这样的:

服务器= myServerName \ theInstanceName

您在上面的声明中使用了正斜杠

 [Input("Server Connection")] [Default("HBSSQL2008/MSSQLSERVER")] //<--This String public InArgument ServerName { get; set; } 

把它改成这个:

 [Input("Server Connection")] [Default("HBSSQL2008\\MSSQLSERVER")] //<--This String public InArgument ServerName { get; set; } 

可能需要双击它以逃避它..

好吧,我想我现在想出来了。

因为我们的服务器上只运行一个SQL实例,所以实际上我不需要使用ServerName \ InstanceName格式。 hbssql2008和HBSSQL2008都允许我连接。 此外,IP也有效。

另外,我遇​​到的另一个问题是我使用的用户名和密码来自Windows Integrated Security,而不是sql server的安全性。 因此,我在服务器上创建了一个具有适当权限和sql安全信息的新帐户,它允许我连接。

感谢大家的帮助!