[英]Database.ExecuteSqlRaw - only accepts non-null SqlParameter type objects
I try to call a method containing code like the one below我尝试调用包含如下代码的方法
The code is to be execute by the procedure代码将由过程执行
Unfortunately, I ran into an error, I described the error below不幸的是,我遇到了一个错误,我在下面描述了错误
does anyone know of any other solution to execute the procedure?有谁知道执行该程序的任何其他解决方案?
Controller: Controller:
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=The SqlParameterCollection only accepts non-null SqlParameter type objects, not > SqlParameter objects.
System.InvalidCastException HResult=0x80004002 Message=SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 > SqlParameter 对象。 Source=Microsoft.Data.SqlClient StackTrace: at Microsoft.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) at Microsoft.Data.SqlClient.SqlParameterCollection.Add(Object value) at > Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter.AddDbParameter(DbComma > nd command, Object value)
Source=Microsoft.Data.SqlClient StackTrace:在 Microsoft.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) 在 Microsoft.Data.SqlClient.SqlParameterCollection.Add(Object value) 在 > Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter.AddDbParameter (DbComma > nd 命令,Object 值)
It might be easier to just call the sproc directly:直接调用 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
Or if you want to make life neater, use interpolated:或者,如果您想让生活更整洁,请使用插值:
SqlParameter idm = new SqlParameter("@whateverName", 0)
_context.Database.ExecuteSqlInterpolated(
$"[dbo].[WstawMail_X] 'ECP', {fromMail}, {toMail}, {title}, {body}, '0', {idm} OUTPUT"
);
//retrieve idm.Value here
If you're using named because that above isn't the order, or you just like to use named, it's a bit clearer with interpolated:如果您使用命名是因为上面不是顺序,或者您只是喜欢使用命名,那么插值会更清楚一些:
_context.Database.ExecuteSqlInterpolated(
$"[dbo].[WstawMail_X] @ExtSystem='ECP', @ParAdresOd={fromMail}, ..., @IDMaila={idm} OUTPUT"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.