2015-01-15 27 views
0

我試圖從SQL Server上的存儲過程中提取DateTime從存儲過程錯誤中獲取DateTime

存儲過程變量:

@TempDateTime DateTime = '' OUTPUT 
SET @TempDateTime = GETDATE() 

線的錯誤出在我的C#CF 2.0計劃

DateTime tempDT = Convert.ToDateTime(sqlcmd.Parameters["@TempDateTime"].Value); 

我碰到下面的錯誤,我在失去它是什麼。我的存儲過程可以發回一個空嗎?我執行存儲過程,並在另一個代碼上運行正常。獲取DateTime並將其插入表中,但是當我嘗試檢索DateTime變種時發生錯誤。

System.IndexOutOfRangeException了未處理
消息= 「一個的{0} {1} '{2}' 不受此包含{3}」。

堆棧跟蹤:
在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

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

您是否將C#代碼中的'@ TempDateTime'參數定義爲輸出? 'Direction = ParameterDirection.Output' –

+0

@JasonFaulkner我只是這樣做了,現在該行代碼得到了與上面相同的錯誤。 – Lgwells1

回答

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; 
+0

就是這樣。我的線看起來有點不同,但現在一切都很好。 sql.cmd.Parameters.Add(「@ TempDateTime」,SqlDbType.DateTime).Direction = ParameterDirection.Output; – Lgwells1

+0

@ Lgwells1 - 很高興這是簡單的。乾杯。 –