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

时间:2012-06-25 15:24:05

标签: c# sql-server-2008 workflow-foundation-4

我正在尝试通过自定义活动使用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<String> JobName { get; set; }

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

    [Input("User Name")]
    [Default("-----")]
    public InArgument<String> UserName { get; set; }

    [Input("Password")]
    [Default("-----")]
    public InArgument<String> 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字符串吗?如果是这样,我怎样才能找到要使用的正确serverName?

感谢您的帮助!

修改 这是一些额外的信息...

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

这是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()

抱歉,我最初没有包含此内容,感谢您的帮助到目前为止

2 个答案:

答案 0 :(得分:2)

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

对于SQL Server 2008,请在同一网站上查看此linky

服务器字符串的部分是:

  

服务器= myServerName \ theInstanceName

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

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

将其更改为:

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

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

答案 1 :(得分:0)

好的,我想我现在已经知道了。

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

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

谢谢大家的帮助!