2

I am trying to query a set of Azure SQL Databases using the sp_execute_remote SQL function with Dapper and DynamicParameters as follows:

Task<List<T>> ExecuteMultiShardCommand<T>(SqlBuilder.Template Template, int Timeout = 120) 
{
    var mParams = new DynamicParameters(Template.Parameters);
    mParams.Add("@datasourcename", "<datasource>");
    mParams.Add("@statement", Template.RawSql, DbType.String);

    using (var conn = new SqlConnection(config["<sqlconnectionstring>"])) 
    {
        var results = await conn.QueryAsync<T>("sp_execute_remote", mParams, commandTimeout: Timeout, commandType: CommandType.StoredProcedure);

        return results.ToList();
    }
}

Running this throws a SQL exception:

System.Data.SqlClient.SqlException : Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

I have not configured Dapper anywhere in my application. Isn't the default handling for DbType.String a nvarchar?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Can this blog give you some ideas https://stackoverflow.com/questions/42455360/procedure-expects-parameter-statement-of-type-ntext-nchar-nvarchar-while-exe ? – Leon Yue Apr 07 '20 at 02:01

0 Answers0