如何使用自定义 DbDataReader 填充 table 值参数?
How to populate a table valued parameter using a custom DbDataReader?
根据 MSDN,System.Data.SqlClient
支持从 DataTable
、DbDataReader
或 IEnumerable<SqlDataRecord>
对象填充 table 值参数。
我编写了以下代码,使用 IEnumerable<SqlDataRecord>
个对象填充 table 值参数:
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 页面显示:
You can also use any object derived from DbDataReader
to stream rows
of data to a table-valued parameter.
我想使用自定义 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()
期间会出现以下异常:
An unhandled exception of type 'System.NotSupportedException' occurred
in System.Data.dll Additional information: Specified method is not
supported.
我已经在我的 CustomDbDataReader
class 的所有方法和属性上设置了一个断点:其中 none 在我得到异常之前被调用所以它可能与实现无关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()
根据 MSDN,System.Data.SqlClient
支持从 DataTable
、DbDataReader
或 IEnumerable<SqlDataRecord>
对象填充 table 值参数。
我编写了以下代码,使用 IEnumerable<SqlDataRecord>
个对象填充 table 值参数:
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 页面显示:
You can also use any object derived from
DbDataReader
to stream rows of data to a table-valued parameter.
我想使用自定义 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()
期间会出现以下异常:
An unhandled exception of type 'System.NotSupportedException' occurred in System.Data.dll Additional information: Specified method is not supported.
我已经在我的 CustomDbDataReader
class 的所有方法和属性上设置了一个断点:其中 none 在我得到异常之前被调用所以它可能与实现无关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()