0
我正在使用實體框架6調用一個存儲過程,該過程使用表值參數。該存儲過程無誤地執行,但不返回任何行。我使用Profiler來跟蹤調用,以便我可以看到EF如何執行它。如果我手動調用存儲過程的方式不同,它將按預期方式返回行。使用表值參數調用存儲過程不會返回行
這裏是我的應用程序代碼:
public IEnumerable<Table1> ListTableValueParameter(IEnumerable<int> lstIDs)
{
//Convert enumerable int to DataTable
System.Data.DataTable dtIDs = new System.Data.DataTable();
dtIDs.Columns.Add("ID", typeof(int));
foreach(int i in lstIDs)
{
dtIDs.Rows.Add(i);
}
var db = new POCDBContext();
//Create parameter for table
System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter();
p.SqlDbType = System.Data.SqlDbType.Structured;
p.ParameterName = "@IDS";
p.Value = dtIDs;
p.TypeName = "dbo.IntegerTableParameter";
using (var connection = db.Database.Connection)
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "EXEC [dbo].pList_TableProperties";
command.Parameters.Add(p);
//Execute stored procedure
using (var reader = command.ExecuteReader())
{
return
((IObjectContextAdapter)db)
.ObjectContext
.Translate<Table1>(reader).ToList();
}
}
}
此代碼執行在運行時,下面的SQL語句,不返回任何記錄:
declare @p3 dbo.IntegerTableParameter
insert into @p3 values(1)
insert into @p3 values(2)
insert into @p3 values(3)
insert into @p3 values(4)
insert into @p3 values(5)
exec sp_executesql N'EXEC [dbo].pList_TableProperties',N'@IDS [dbo].[IntegerTableParameter] READONLY',@[email protected]
如果我運行下面的SQL語句,它將返回記錄:
declare @p3 dbo.IntegerTableParameter
insert into @p3 values(1)
insert into @p3 values(2)
insert into @p3 values(3)
insert into @p3 values(4)
insert into @p3 values(5)
EXEC [dbo].pList_TableProperties @p3
有什麼不同我可以在客戶端做得到th是否正確執行?
'command.CommandText = 「[DBO] .pList_TableProperties」; command.CommandType = CommandType.StoredProcedure'不應該真的解決你的問題,但不管做什麼都很好。 –
其實@JeroenMostert我認爲這將解決這個問題。由於該命令未設置爲StoreProcedure,並且在傳遞查詢中沒有提及該參數,因此它將被添加到該命令中,但其行爲表現爲完全不傳遞參數。 –
@JeroenMostert:我認爲你的解決方案是正確的。另一個選項:'command.CommandText =「EXEC [dbo] .pList_TableProperties @IDS」;'而不是'command.CommandText =「EXEC [dbo] .pList_TableProperties」;' – TriV