简体   繁体   English

Database.ExecuteSqlRaw - 只接受非空的 SqlParameter 类型对象

[英]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.

相关问题 SqlParameterCollection 只接受非空的 SqlParameter 类型对象,而不接受 Microsoft.Data.SqlClient 中的 SqlParameter 对象 - The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects in Microsoft.Data.SqlClient System.InvalidCastException:“SqlParameterCollection 只接受非空 SqlParameter 类型对象,不接受 SqlParameter 对象。” - System.InvalidCastException: 'The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.' SqlParameterCollection只接受非null的SqlParameter类型对象,而不接受DBNull对象 - SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects sqlparametercollection 只接受非空的 sqlparameter 类型对象,而不是 byte[] 对象 - the sqlparametercollection only accepts non-null sqlparameter type objects not byte[] objects 错误:SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受String对象 - Error : The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects SqlParameterCollection仅接受非null SqlParameter类型的对象,不接受DBNull对象 - The SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects “SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 String 对象” - “SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects” SqlParameterCollection仅接受非空的SqlParameter类型对象。 参数名称:值 - The SqlParameterCollection only accepts non-null SqlParameter type objects. Parameter name: value SqlParameterCollection 只接受非空的 Parameter 类型对象 - The SqlParameterCollection only accepts non-null Parameter type objects SqlParameterCollection仅接受非空的SqlParamter类型对象,而不接受String对象 - The SqlParameterCollection only accepts non-null SqlParamter type objects, not String objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM