Eseosa Omoregie
Eseosa Omoregie

Reputation: 301

cannot convert from 'System.Data.SqlClient.SqlConnection' to 'Microsoft.SqlServer.Management.Common.IRenewableToken'

I am currenty creating SQLWorker class with various methods which excutes various sql commands as part of a wider framework. I have a method which is used to execute a sql scripts. as whon below:

public object ExecuteScript(string sql, ExecutionType executionType = ExecutionType.NonQuery,
        int statementTimeout = 1800)
    {

        using (var connection = new SqlConnection(ConnectionString))
        {
            connection.InfoMessage += (sender, args) =>
                _infoMessageText.AppendLine(args.Message);

            var server = new Server(new ServerConnection(connection));
            server.ConnectionContext.StatementTimeout = statementTimeout;
            switch (executionType)
            {
                case ExecutionType.NonQuery:
                    return server.ConnectionContext.ExecuteNonQuery(sql);
                case ExecutionType.Scalar:
                    return server.ConnectionContext.ExecuteScalar(sql);
                case ExecutionType.Reader:
                    return server.ConnectionContext.ExecuteReader(sql);
            }
        }
        return null;
    }

I am currently getting the error: Argument 1: cannot convert from 'System.Data.SqlClient.SqlConnection' to 'Microsoft.SqlServer.Management.Common.IRenewableToken'

the particular line is

 var server = new Server(new ServerConnection(connection));

The ServerConnection Class seem to expect a parameter of IRenewableToken but it has constructor that accepts a SqlConnection parameter.

I am not sure what I missing I beleive I have the correct NugetPackages and have the following using statements

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

Any ideas on what I am missing here?

Upvotes: 2

Views: 5487

Answers (1)

Eseosa Omoregie
Eseosa Omoregie

Reputation: 301

This was solved by changing the using statement from using System.Data.SqlClient to Microsoft.Data.SqlClient. I was refactoring an old version of code and they were using an older version of Microsoft.Sqlserver.Management.objects nuget package along with System.Data.SqlClient. With the latest version of Microsoft.Sqlserver.Management.objects it didnt work.

Upvotes: 9

Related Questions