Mad*_* Zu 1 .net c# sql sql-server oledb
从C#调用存储过程时出现以下错误.
参数在代码中定义为:
if (repRIS.Length > 0)
command.Parameters.AddWithValue("@repRIS", repRIS);
else
command.Parameters.AddWithValue("@repRIS", DBNull.Value);
command.Parameters.Add("@invDt", OleDbType.Date).Value = invDate;
Run Code Online (Sandbox Code Playgroud)
我已经注释掉了存储过程中的所有内容,现在只有以下内容:
ALTER PROCEDURE [dbo].[SearchDates]
-- Add the parameters for the stored procedure here
@invDt datetime,
@repRIS varchar(10) ,
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select this, that, and, the, other from myTableName ird
where
ird.RIS = COALESCE(@repRIS, ird.RIS)
and ird.InventoryDate = @invDt
END
Run Code Online (Sandbox Code Playgroud)
InventoryDate的类型为数据库中的DateTime.
当我从SQL MS运行SP时,它会产生没有问题的结果,但是,当使用应用程序调用它时,我收到以下错误消息
将数据类型nvarchar转换为datetime时出错
我注意到它从SQLConnection切换到OLEDBConnection后开始发生.我还没有证实这一点.(我不得不切换到遵守我之前写的其他应用程序的方式)
更新:当我在文本框中输入9/30/2018的值时,它将被传递到存储过程中:{9/30/2018 12:00:00 AM}转换为datetime后(上面的代码)
使用OleDb时,应始终记住参数不是根据它们的名称传递的,而是按照它们添加到Parameters集合的确切顺序传递的.
在您的代码中,首先添加@repRIS,这是传递给SP的第一个参数.但SP期望第一个参数的日期,你得到例外
您需要更改Parameters集合中插入的顺序或切换SP中参数的声明顺序
command.Parameters.Add("@invDt", OleDbType.Date).Value = invDate;
if (repRIS.Length > 0)
command.Parameters.AddWithValue("@repRIS", repRIS);
else
command.Parameters.AddWithValue("@repRIS", DBNull.Value);
Run Code Online (Sandbox Code Playgroud)
另一件事是看看这篇文章我们可以停止使用AddWithValue吗?
| 归档时间: |
|
| 查看次数: |
247 次 |
| 最近记录: |