[英]ExecuteSqlRaw parametric query in .NET Core 3
I'm trying to execute a parametric query in C#, the code is this:我正在尝试在 C# 中执行参数查询,代码是这样的:
string sql = @"INSERT INTO [dbo].[ShoppingListShop] " +
"([PlaceId], " +
"[Name], " +
"[Address], " +
"[Location], " +
"[NorthEast], " +
"[SouthWest], " +
"[Date], " +
"[LocationLat], " +
"[LocationLng], " +
"[NorthEastLat], " +
"[NorthEastLng], " +
"[SouthWestLat], " +
"[SouthWestLng])" +
"VALUES(" +
"@placeId, " +
"@name, " +
"@address, " +
"GEOGRAPHY::Point(@lat, @lng, 4326), " +
"GEOGRAPHY::Point(@nelat, @nelng, 4326), " +
"GEOGRAPHY::Point(@swlat, @swlng, 4326), " +
"SYSDATETIMEOFFSET(), " +
"@lat, " +
"@lng, " +
"@nelat, " +
"@nelng, " +
"@swlat, " +
"@swlng);";
SqlParameter placeIdPrm = new SqlParameter("@placeId", SqlDbType.NVarChar);
SqlParameter namePrm = new SqlParameter("@name", SqlDbType.NVarChar);
SqlParameter addressPrm = new SqlParameter("@address", SqlDbType.NVarChar);
SqlParameter latPrm = new SqlParameter("@lat", SqlDbType.Decimal, 18);
SqlParameter lngPrm = new SqlParameter("@lng", SqlDbType.Decimal, 18);
SqlParameter neLatPrm = new SqlParameter("@nelat", SqlDbType.Decimal, 18);
SqlParameter neLngPrm = new SqlParameter("@nelng", SqlDbType.Decimal, 18);
SqlParameter swLatPrm = new SqlParameter("@swlat", SqlDbType.Decimal, 18);
SqlParameter swLngPrm = new SqlParameter("@swlng", SqlDbType.Decimal, 18);
placeIdPrm.Value = shop.PlaceId;
namePrm.Value = shop.Name;
addressPrm.Value = shop.Address;
latPrm.Value = shop.LocationLat;
lngPrm.Value = shop.LocationLng;
neLatPrm.Value = shop.NorthEastLat;
neLngPrm.Value = shop.NorthEastLng;
swLatPrm.Value = shop.SouthWestLat;
swLngPrm.Value = shop.SouthWestLng;
SqlParameter[] parametri = new SqlParameter[9] { placeIdPrm, namePrm, addressPrm, latPrm, lngPrm, neLatPrm, neLngPrm, swLatPrm, swLngPrm };
sqlServerDbContext.Database.ExecuteSqlRaw(sql, parametri);
For some reason I get this exception出于某种原因,我得到了这个例外
The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.
SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 SqlParameter 对象。
What am I doing wrong?我究竟做错了什么?
This is a shot in the dark based on the error message, but: is it possible that you are using both System.Data.SqlClient
and Microsoft.Data.SqlClient
?这是根据错误消息在黑暗中拍摄的,但是:您是否可能同时使用
System.Data.SqlClient
和Microsoft.Data.SqlClient
? ie sqlServerDbContext.Database.ExecuteSqlRaw
is expecting a System.Data.SqlClient.SqlParameter[]
(and instances), and you've given it a Microsoft.Data.SqlClient.SqlParameter[]
(and instances)?即
sqlServerDbContext.Database.ExecuteSqlRaw
需要System.Data.SqlClient.SqlParameter[]
(和实例),而您已经给了它一个Microsoft.Data.SqlClient.SqlParameter[]
(和实例)? (or vice versa) - try "go to definition" on SqlParameter
, and see where it is coming from. (反之亦然) - 尝试在
SqlParameter
上“转到定义”,看看它来自哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.