question

EseosaOmoregie-1710 avatar image
0 Votes"
EseosaOmoregie-1710 asked JonFrisco-8106 commented

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?


vs-debugging
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DylanZhu-MSFT avatar image
2 Votes"
DylanZhu-MSFT answered JonFrisco-8106 commented

Hi EseosaOmoregie,

Thank you for feedback.

I have reproduced this issue on my side. Please change System.Data.SqlClient to Microsoft.Data.SqlClient, which is like:

 //using System.Data.SqlClient;
 using Microsoft.Data.SqlClient;

And it could be solved on my side.

Best Regards,
Dylan


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks from me, as well! I ran into the same issue today and changing to Microsoft.Data.SqlClient resolved it for me, too.

0 Votes 0 ·
EseosaOmoregie-1710 avatar image
0 Votes"
EseosaOmoregie-1710 answered

@DylanZhu-MSFT

That indeed solved issue. Thank you for the response. 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. Much appreciated

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.