我嘗試呼叫包含如下代碼的方法
代碼由程式執行
不幸的是,我遇到了錯誤,我在下面描述了錯誤
有誰知道執行該程式的任何其他解決方案?
控制器:
string fromMail = "test";
string toMail= "test";
string title = "test";
string body= "test";
string sql = @"
USE [eFaktura]
GO
DECLARE @ExtSystem varchar(20)
DECLARE @AdresOd varchar(255)
DECLARE @AdresDo varchar(255)
DECLARE @Temat varchar(255)
DECLARE @Tresc varchar(max)
DECLARE @Logo bit
DECLARE @IDMaila int
EXECUTE [dbo].[WstawMail_X]
@ExtSystem = @ParExtSystem
,@AdresOd = @ParAdresOd
,@AdresDo = @ParAdresDo
,@Temat = @ParTemat
,@Tresc = @ParTresc
,@Logo = 0
,@IDMaila = @IDMaila output
GO
";
_context.Database.ExecuteSqlRaw(sql,
new SqlParameter("@ParExtSystem", "ECP"),
new SqlParameter("@ParAdresOd", fromMail),
new SqlParameter("@ParAdresDo", toMail),
new SqlParameter("@ParTemat", title),
new SqlParameter("@ParTresc", body),
new SqlParameter("@ParLogo", 0)
);
System.InvalidCastException HResult=0x80004002 Message=SqlParameterCollection 只接受非空 SqlParameter 型別的物件,而不是 > SqlParameter 物件。Source=Microsoft.Data.SqlClient StackTrace:在 Microsoft.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) at Microsoft.Data.SqlClient.SqlParameterCollection.Add(Object value) at > Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter.AddDbParameter (DbComma > nd 命令,物件值)
uj5u.com熱心網友回復:
直接呼叫 sproc 可能更容易:
SqlParameter idm;
_context.Database.ExecuteSqlRaw(
"[dbo].[WstawMail_X] @pEx, @pAOd, @pADo, @pTe, @pTr, @pL, @pIDM OUTPUT",
new SqlParameter("@pEx", "ECP"),
new SqlParameter("@pAOd", fromMail),
new SqlParameter("@pADo", toMail),
new SqlParameter("@pTe", title),
new SqlParameter("@pTR", body),
new SqlParameter("@pL", 0),
idm = new SqlParameter("@pIDM", 0)
);
//you can retrieve the value of pIDM here if you want
或者,如果您想讓生活更整潔,請使用插值:
SqlParameter idm = new SqlParameter("@whateverName", 0)
_context.Database.ExecuteSqlInterpolated(
$"[dbo].[WstawMail_X] 'ECP', {fromMail}, {toMail}, {title}, {body}, '0', {idm} OUTPUT"
);
//retrieve idm.Value here
如果您使用命名是因為上面不是順序,或者您只是喜歡使用命名,則使用插值會更清楚:
_context.Database.ExecuteSqlInterpolated(
$"[dbo].[WstawMail_X] @ExtSystem='ECP', @ParAdresOd={fromMail}, ..., @IDMaila={idm} OUTPUT"
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/388555.html
上一篇:基于唯一值計數的JS物件過濾器
