2014-10-20 72 views
1

代碼像this使用表值參數(TVP),而不是 '在哪裏'

connection.Execute("delete from Table where ID in @ids", new { ids=listOfIds }); 

listOfIds太長失敗。你會得到類似的規定:

The incoming request has too many parameters. The server supports a maximum of 2100 

(取決於您的RDBMS)

理想情況下,我想用一個表值參數。我還沒有找到一個體面的小巧的例子。有人能指點我正確的方向嗎?

+0

我目前看Dapper.Tvp:http://www.nuget.org/packages/Dapper.Tvp這一點。 – cs0815 2014-10-20 15:47:20

+0

這是否顯示用法? http://stackoverflow.com/questions/6232978/does-dapper-support-sql-2008-table-valued-parameters – 2014-10-20 16:55:52

+0

謝謝我意識到這一點,但沒有任何意義。我想要一個名爲@Ids的TVP的內連接,它使用了'WHERE Id IN(@Ids)' - 沒有任何內容... – cs0815 2014-10-20 16:59:26

回答

2

這應有助於:

// 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 
相關問題