What string do I use to connect to an SQL Server?

2019-03-04 02:46发布

I'm trying to execute an SQL Job using a CRM Workflow by making a custom activity. In my code, an online tutorial had me construct it like this (in 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();
            }
        }
    }
}

However, when I try to run this workflow, it throws an error saying it couldn't connect to the server. (Specifically, the error is thrown on the server.ConnectionContext.Connect() method call).

Is the problem the serverName String? If so, how can I find out the correct serverName to use?

Thanks for any help!

EDIT: Here's some additional info...

This is how I got the server and instance name the first time: My SQL Query

And here's the error on the CRM Workflow:

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

Sorry I didn't include this originally, thanks for your help so far

2条回答
再贱就再见
2楼-- · 2019-03-04 03:25

Although not entirely relevant to the question but nonetheless, a useful site to bookmark to aid in your getting the connection string right.

For SQL Server 2008, see this linky here, on the same site

Portion of the server string is this:

Server=myServerName\theInstanceName

You have used the forward slash in your declarative above

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

Change it to this:

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

Might need to double-slash it to escape it..

查看更多
等我变得足够好
3楼-- · 2019-03-04 03:52

Ok, I think I figured it out now.

Because there is only one instance of SQL running on our server, I don't actually need to use the ServerName\InstanceName format. Both hbssql2008 and HBSSQL2008 allow me to connect. In addition, the IP works as well.

Also, another issue I came across was the the user name and password I was using was from Windows Integrated Security, not the sql server's security. Therefore, I created a new account on the server with appropriate permissions and sql security info and it allowed me to connect.

Thank you all for your help!

查看更多
登录 后发表回答