我试图从我的SQL Server上的存储过程中提取DateTime
。
存储过程变量:
@TempDateTime DateTime = '' OUTPUT
SET @TempDateTime = GETDATE()
在我的C#CF 2.0程序中输出错误
DateTime tempDT = Convert.ToDateTime(sqlcmd.Parameters["@TempDateTime"].Value);
我收到以下错误,而我却迷失了它。我的存储过程可以发送回null吗?我执行存储过程,它在另一段代码上运行正常。获取DateTime
并将其插入表中,但在尝试检索DateTime
var时出错。
System.IndexOutOfRangeException未处理
消息=“此{3}不包含{1}'{2}'的{0}。”堆栈跟踪:
在System.Data.SqlClient.SqlParameterCollection.RangeCheck()
在System.Data.SqlClient.SqlParameterCollection.get_Item()
在Missouri_Scanning_Utility.Form1.updateDatabase()
在Missouri_Scanning_Utility.Form1.tmrUpdateDatabase_Tick()
在System.Windows.Forms.Timer._WnProc()
在System.Windows.Forms.ApplicationThreadContext._InternalContextMessages()
在Microsoft.AGL.Forms.EVL.EnterMainLoop()
在System.Windows.Forms.Application.Run()
在Missouri_Scanning_Utility.Program.Main()
答案 0 :(得分:0)
当您为调用发送中定义参数值时,您必须相应地声明OUTPUT
参数。
由于您没有发布代码的相关部分,以下是执行此操作的一般步骤:
var tempDateTimeParam = new SqlParameter("@TempDateTime", SqlDbType.DateTime)
{
Direction = ParameterDirection.Output
};
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(tempDateTimeParam);
// Add additional params.
// Execute stored proc.
// Read result from output param which should now have a value.
var tempDateTimeValue = (DateTime)tempDateTimeParam.Value;