0

我正在尝试通过进行自定义活动来使用 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 字符串吗?如果是这样,我怎样才能找到要使用的正确服务器名称?

谢谢你的帮助!

编辑: 这里有一些额外的信息......

这是我第一次获得服务器和实例名称的方式: 我的 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()

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

4

2 回答 2

2

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

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

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

服务器=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; }

可能需要双斜线来逃避它..

于 2012-06-25T15:30:31.850 回答
0

好的,我想我现在想通了。

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

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

谢谢大家的帮助!

于 2012-06-29T13:27:26.267 回答