[英]Strongly typed dataset as user defined table type parameter
I have a stored procedure to handle Inserts, Updates and Deletes to a table defined as 我有一个存储过程来处理插入,更新和删除到定义为的表
CREATE TABLE [dbo].[TestTable](
[Id] [int] PRIMARY KEY NOT NULL,
[Data] [nvarchar](50) NOT NULL,
[ChangeDate] [datetime] NULL)
with the stored prodedure 与存储的prodedure
CREATE PROCEDURE MergeTest
@Testing TestTableType readonly
AS
BEGIN
MERGE INTO Testing as Target
USING (SELECT * FROM @Testing) AS SOURCE
ON (Target.Id = Source.Id)
WHEN MATCHED THEN
UPDATE SET
Target.Data = Source.Data,
Target.ChangeDate = Source.ChangeDate
WHEN NOT MATCHED BY TARGET THEN
INSERT (Data, ChangeDate)
VALUES (Source.Data, Source.ChangeDate)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
RETURN 0;
END
and the UDT Type as 和UDT类型为
CREATE TYPE TestTableType AS TABLE(
[Id] [int] PRIMARY KEY NOT NULL,
[Data] [nvarchar](50) NOT NULL,
[ChangeDate] [datetime] NULL)
I am trying to use this structure to do bulk inserts, etc from C#. 我正在尝试使用此结构从C#执行批量插入等。 Using the following code it works:
使用以下代码:
using (SqlConnection connection = new SqlConnection(@"..."))
{
connection.Open();
DataTable DT = new DataTable();
DT.Columns.Add("Id", typeof(int));
DT.Columns.Add("Data", typeof(string));
DT.Columns.Add("ChangeDate", typeof(DateTime));
for (int i = 0; i < 100000; i++)
{
DT.AddTestRow((i + 1), (i + 1).ToString(), DateTime.Now);
}
using (SqlCommand command = new SqlCommand("MergeTest", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Testing", DT);
command.ExecuteNonQuery();
}
}
However, when i change the lines 但是,当我改变线条
DataTable DT = new DataTable();
DT.Columns.Add("Id", typeof(int));
DT.Columns.Add("Data", typeof(string));
DT.Columns.Add("ChangeDate", typeof(DateTime));
to 至
DataSet1.TestDataTable DT = new DataSet1.TestDataTable();
which is a strongly typed version of the same DataTable
structure I get a Argument Exception
Error of 这是同一个
DataTable
结构的强类型版本我得到了一个Argument Exception
错误
No mapping exists from object type TestBulkInsertDataset.DataSet1+TestDataTable to a known managed provider native type.
从对象类型TestBulkInsertDataset.DataSet1 + TestDataTable到已知的托管提供程序本机类型不存在映射。
Is there any way to use a strongly typed DataTable
as a User Defined Table Type Parameter? 有没有办法使用强类型
DataTable
作为用户定义的表类型参数?
Found the answer. 找到了答案。 When using strongly typed Data Tables you have to specify the type for the parameter.
使用强类型数据表时,必须指定参数的类型。 The parameter line becomes:
参数行变为:
var testingparam = command.Parameters.AddWithValue("@Testing", DT);
testingparam.SqlDbType = SqlDbType.Structured;
and then it all works. 然后一切正常。
Have you tried something like this? 你尝试过这样的事吗?
DataSet1.TestDataTable DT = new DataSet1.TestDataTable();
// Fill data table
DataTable DT1 = DT; // DataSet1.TestDataTable should be a subclass of DataTable
using (SqlCommand command = new SqlCommand("MergeTest", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Testing", DT1);
command.ExecuteNonQuery();
}
If typed DataSets are still subclasses of regular DataSets and DataTables, I would think this would work. 如果键入的DataSet仍然是常规DataSet和DataTables的子类,我认为这样可行。
ETA: Since that doesn't work, how about this? ETA:既然这不起作用,那怎么样?
DataSet DS1 = new DataSet();
DS1.Merge(DT, false, MissingSchemaAction.Add);
// etc.
command.Parameters.AddWithValue("@Testing", DS1.Tables[0]);
Assuming that works (and you may have to fiddle with overloads of the Merge method to get the desired result) you'll get a DataTable in your DataSet that has the schema and data of DT, the DataSet1.TestDataTable
, but will just be of type DataTable
. 假设有效(并且您可能不得不使用Merge方法的重载来获得所需的结果),您将在DataSet中获得一个DataTable,它具有DT的架构和数据,即
DataSet1.TestDataTable
,但它只是输入DataTable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.