【发布时间】:2020-10-09 02:03:36
【问题描述】:
我正在尝试将字符串参数传递给 SQL 查询, 接收错误如下。我该如何解决这个问题?目前正在使用答案EF Core 2.2, Passing String Parameter to FromSql Statement
输入字符串的格式不正确。
public async Task<IEnumerable<Product>> GetProduct(string productKey)
{
var productParameter = new SqlParameter("@productKey", SqlDbType.VarChar);
productParameter.Value = productKey;
var productIdList = db.TestDb
.FromSql($"select ProductId from dbo.Product product" +
" (where product.ProductKey = {productParameter})" )
.Select(c => c.ProductId).ToList();
它是来自 ProductKey 的 varchar(6) 类型
使用 Net Core 2.2
【问题讨论】:
-
您需要确定要选择哪些列,并在表名前使用关键字 FROM。 SELECT something FROM table WHERE stuff
-
你的意思是把productKey赋值为参数值吗?你有 productNumber,我看不出它是在哪里定义的。
-
你的数据库中ProductKey的数据类型是什么?
-
您链接到的问题的公认答案是错误的,因此您的代码也是错误的。
-
旁注:您不需要
.Select(c => c.ProductId),因为您在查询中指定了列名,并且它是您要返回的唯一列。也许'.ToList()'?
标签: c# .net entity-framework .net-core .net-core-2.2