c# - SqlParameterCollection 只接受非空参数类型对象
问题描述
我正在尝试在 Entity Framework Core 中使用存储过程。执行存储过程时,我传递了两个输入参数和一个输出参数。我不断收到此错误:
SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 SqlParameter 对象。
这是我的代码:
public string InsertCardsData(DateTime RecordingStartDate, DateTime RecordingEndDate)
{
try
{
// DateTime DRecEndDate = Convert.ToDateTime(RecordingEndDate);
DateTime DRecEndDate = RecordingEndDate.AddDays(1);
var RecordStartDate = new SqlParameter
{
ParameterName = "RecordingStartDate",
Value = RecordingStartDate,
Direction = ParameterDirection.Input
};
var RecordEndDate = new SqlParameter
{
ParameterName = "RecordingEndDate",
Value = DRecEndDate,
Direction = ParameterDirection.Input
};
var RecordCount = new SqlParameter
{
ParameterName = "RecLoadCount",
Direction = ParameterDirection.Output
};
var SQL = "Exec Recload_InsertPrimeExtract @RecordingStartDate, @RecordingEndDate, @RecLoadCount OUT";
var result = _context.Database.ExecuteSqlRaw(SQL, RecordStartDate, RecordEndDate, RecordCount);
var RecordCountValue = RecordCount.Value.ToString();
return RecordCountValue;
}
catch (Exception ex)
{
return "";
}
}
我将放置一个有意义的 catch 语句,但是现在,我在 catch 语句中放置了一个断点,并且发生了上述错误。
任何帮助将不胜感激。
解决方案
您的问题很可能是由于EF Core 3 中的重大更改引起的,通过声明using
System.Data.SqlClient
应该何时using Microsoft.Data.SqlClient
为什么
Microsoft.Data.SqlClient
是 SQL Server 未来的旗舰数据访问驱动程序,System.Data.SqlClient
不再是开发重点。某些重要功能(例如始终加密)仅在Microsoft.Data.SqlClient
.
此外
如果您的代码直接依赖于
System.Data.SqlClient
,则必须将其更改为引用Microsoft.Data.SqlClient
;由于这两个包保持了非常高的 API 兼容性,这应该只是一个简单的包和命名空间更改。
github上的相关问题
推荐阅读
- c# - Google Maps API - 从 JSON 响应中读取地点详细信息
- r - 并行化采用外部指针的函数 (XPtr)
- node.js - 当我有额外的不等式 where() 标准时,如何从 firestore 集合中获取随机文档?
- cross-compiling - 为 QNX 编译 LAME
- apache-spark - 如何使用纱线配置 24/7 运行 Spark 流
- python - 从数据框列获取数据作为元组列表
- android - 如何在滚动视图中设置当前项目的背景颜色
- mysql - 使用 Cloud Formation 将 MySQL RDS 存储类型从磁性更改为 SSD/预置 IOPS
- kubectl - 重复 watcher.go:208] watch chan 错误:etcdserver: mvcc: required revision has been compacted
- android - 使用 ExoPlayer 的自适应 .mp4 视频流