這應有助於:
// 1. declare the custom data type
// this is just to make it re-runnable; normally you only do this once
try { connection.Execute("drop type MyIdList"); } catch { }
connection.Execute("create type MyIdList as table(id int);");
// 2. prepare the data; if this isn't a sproc, also set the type name
DataTable ids = new DataTable {
Columns = {{"id", typeof(int)}},
Rows = {{1},{3},{5}}
};
ids.SetTypeName("MyIdList");
// 3. run the query, referencing the TVP (note @tmp represents the db data)
int sum = connection.Query<int>(@"
-- spoof some data
declare @tmp table(id int not null);
insert @tmp (id) values(1), (2), (3), (4), (5), (6), (7);
-- the actual query
select * from @tmp t inner join @ids i on i.id = t.id", new { ids }).Sum();
sum.IsEqualTo(9); // just checks the result
我目前看Dapper.Tvp:http://www.nuget.org/packages/Dapper.Tvp這一點。 – cs0815 2014-10-20 15:47:20
這是否顯示用法? http://stackoverflow.com/questions/6232978/does-dapper-support-sql-2008-table-valued-parameters – 2014-10-20 16:55:52
謝謝我意識到這一點,但沒有任何意義。我想要一個名爲@Ids的TVP的內連接,它使用了'WHERE Id IN(@Ids)' - 沒有任何內容... – cs0815 2014-10-20 16:59:26