c#中数据库调用抛出意外错误
本文关键字:意外 错误 调用 数据库 | 更新日期: 2023-09-27 18:16:35
我已经编写了以下方法来从数据库中获取对象列表:
public IEnumerable<ProductDBKey6> AssignProductKey6 (string strProdList)
{
List<ProductDBKey6> lstProdDBKey6 = new List<ProductDBKey6>();
// Create the parameters collection
var parameters = new Collection<SqlParameter>();
// Add each parameter. Entity will not work without all params in the correct order
SqlParameter param = StoredProcedureParameterBuilder.StringParam("@ProductID", strProdList, -1);
int? tempTimeout = this.Database.CommandTimeout;
this.Database.CommandTimeout = 300;
lstProdDBKey6 = this.Database.SqlQuery<ProductDBKey6>("spc.FindProductDBKeyForID", param).ToList();
this.Database.CommandTimeout = tempTimeout;
return lstProdDBKey6;
}
但是我收到了@ProductID参数没有提供的错误。以下是StoredProcedureParameterBuilder类中编写的代码:
internal static class StoredProcedureParameterBuilder
{
internal static SqlParameter StringParam(string paramName, string paramValue, int paramSize)
{
SqlParameter outParam = new SqlParameter
{
ParameterName = paramName,
SqlDbType = System.Data.SqlDbType.VarChar,
Direction = System.Data.ParameterDirection.Input,
Size = paramSize
};
if (string.IsNullOrEmpty(paramValue))
{
outParam.Value = DBNull.Value;
}
else
{
outParam.Value = paramValue;
}
return outParam;
}
}
存储过程只接受一个参数@ProductID。下面是存储过程声明:
ALTER PROCEDURE [spc].[FindProductDBKeyForID] (
@ProductID VARCHAR(MAX)
)
请帮忙解决这个错误
代替这一行:
lstProdDBKey6 =
this
.Database.SqlQuery<ProductDBKey6>("spc.FindProductDBKeyForID", param)
.ToList();
试试这个:
lstProdDBKey6 =
this
.Database.SqlQuery<ProductDBKey6>("exec spc.FindProductDBKeyForID @ProductID", param)
.ToList();