我有一個帶有3個輸入參數的存儲過程。Dapper:幫助我使用多個用戶定義的表類型運行存儲過程
... PROCEDURE [dbo].[gama_SearchLibraryDocuments]
@Keyword nvarchar(160),
@CategoryIds [dbo].[IntList] READONLY,
@MarketIds [dbo].[IntList] READONLY ...
其中IntList是用戶定義的表類型。
CREATE TYPE [dbo].[IntList]
AS TABLE ([Item] int NULL);
我的目標是用精巧的方式調用這個存儲過程。
我發現了一些關於傳遞用戶定義類型與精巧的例子。 其中之一是在Dapper.Microsoft.Sql nuget包中實現的TableValuedParameter類。
var list = conn.Query<int>("someSP", new
{
Keyword = (string)null,
CategoryIds = new TableValuedParameter<int>("@CategoryIds", "IntList", new List<int> { }),
MarketIds = new TableValuedParameter<int>("@MarketIds", "IntList", new List<int> { 541 })
}, commandType: CommandType.StoredProcedure).ToList();
上面寫代碼拋出
An exception of type 'System.NotSupportedException' occurred in Dapper.dll but was not handled in user code
Additional information: The member CategoryIds of type Dapper.Microsoft.Sql.TableValuedParameter`1[System.Int32] cannot be used as a parameter value
我測試我的與一個用戶定義的表類型的存儲過程和它工作得很好。
conn.Query<int>("someSP", new TableValuedParameter<int>("@MarketIds", "IntList", new List<int> { 541 }), commandType: CommandType.StoredProcedure).ToList();
我需要運行原始存儲過程的幫助。 謝謝。
我想多一點的解釋將是很好:) – DatRid
小巧玲瓏具有擴展方法與表值參數的工作。公共靜態SqlMapper.ICustomQueryParameter AsTableValuedParameter(此DataTable表,字符串typeName = null) – Dmitry
只需將解釋添加到答案:)順便說一句好的答案!它只是純代碼的答案大多不會幫助一般,所以更多你給的解釋,對其他用戶更有用! – DatRid