【问题标题】:How to populate a table valued parameter using a custom DbDataReader?如何使用自定义 DbDataReader 填充表值参数?
【发布时间】:2017-03-21 00:50:36
【问题描述】:

根据MSDN,System.Data.SqlClient支持从DataTable、DbDataReader或IEnumerable<SqlDataRecord>对象填充表值参数。

我编写了以下代码,它使用IEnumerable<SqlDataRecord> 对象填充表值参数:

static void Main()
{       
    List<int> idsToSend = ...
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();    
        SqlCommand command = new SqlCommand(@"SELECT ...
                                              FROM ... 
                                              INNER JOIN @ids mytable ON ...", connection);

        command.Parameters.Add(new SqlParameter("@ids", SqlDbType.Structured)
        {
            TypeName = "int_list_type",
            Direction = ParameterDirection.Input,
            Value = GetSqlDataRecords(idsToSend) //returns IEnumerable<SqlDataRecord> 
        });

        SqlDataReader reader = command.ExecuteReader();
        //consume reader...
    }
}

完美运行。

MSDN 页面显示:

您还可以使用从DbDataReader 派生的任何对象来流式传输行 将数据转换为表值参数。

我想使用自定义DbDataReader 来传递行数据。像这样的:

public class CustomDataReader : DbDataReader
{
    public CustomDataReader(IEnumerable<int> values)
    {           
    }

    //implementation of other abstract methods and fields required by DbDataReader
    //...
}

我在初始代码示例中更改了以下行:

Value = GetSqlDataRecords(idsToSend)
=>
Value = new CustomDataReader(idsToSend)

如果我运行代码,它会在执行 command.ExecuteReader() 期间给我以下异常:

发生“System.NotSupportedException”类型的未处理异常 在 System.Data.dll 附加信息:指定的方法不是 支持。

我在我的CustomDbDataReader 类的所有方法和属性上都设置了断点:在我得到异常之前它们都没有被调用,所以它可能与DbDataReader 的实现无关


编辑:这是堆栈跟踪,根据要求:

   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at ConsoleApplication211.Program.Main() in Program.cs:line 51
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 能否编辑问题以显示异常的堆栈跟踪
  • @stuartd:完成
  • 我能看到的唯一区别是它使用存储过程,这是使用DbDataReader 的要求吗?

标签: c# sql-server sqlclient table-valued-parameters dbdatareader


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2012-07-06
    • 2016-12-02
    • 1970-01-01
    • 2013-05-23
    • 2013-06-21
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多