2011-11-10 76 views
0

我在SQL Server中有一個表,其名稱爲,其類型爲datetime。我也有一個C#結構映射到該表中的單個記錄;在這個結構中是DateTime類型的名爲m_DateTime的成員。之後我從這個表中檢索記錄,使用DataSet,然後嘗試獲取來自Datasetsql_DateTime到我m_DateTime變量發生如何使用c#和數據集來讀/寫DateTime到SQL Server

我的問題。當我嘗試執行類似於處理其他數據類型的方式時,我得到了一個InvalidCastException

我希望能夠在我的GUI中使用DateTimePicker來顯示和設置日期和時間。

我的代碼已附上供您參考。感謝您的任何指導。

public bool GetExperiment(ref Experiment exp, int ExperimentID, ref string statusMsg) 
    { 
     bool ret = true; 
     statusMsg = "GetExperiment: "; 

     try 
     { 
      // Open the connection 
      conn.Open(); 

      // init SqlDataAdapter with select command and connection 
      string SelectString = 
       @"SELECT * " + 
       "FROM Experiment " + 
       "WHERE ExperimentID = " + ExperimentID.ToString(); 

      SqlDataAdapter daExperiments = new SqlDataAdapter(SelectString, conn); 

      // fill the dataset 
      DataSet dsExperiments = new DataSet(); 
      daExperiments.Fill(dsExperiments, "Experiment"); 

      // assign dataset values to proj object that was passed in 
      exp.m_ExperimentID = (int)dsExperiments.Tables["Experiment"].Rows[0]["ExperimentID"]; 
      exp.m_ProjectID = (int)dsExperiments.Tables["Experiment"].Rows[0]["ProjectID"]; 
      exp.m_Name = (string)dsExperiments.Tables["Experiment"].Rows[0]["Name"]; 
      exp.m_Description = (string)dsExperiments.Tables["Experiment"].Rows[0]["Description"]; 
      exp.m_UserID = (int)dsExperiments.Tables["Experiment"].Rows[0]["UserID"]; 

      // PROBLEM OCCURS HERE 
      exp.m_DateTime = (DateTime)dsExperiments.Tables["Experiment"].Rows[0]["DateTime"]; 

     } 
     catch (Exception ex) 
     { 

      ret = false; 
      statusMsg += "Failed - " + ex.Message; 
     } 
     finally 
     { 

      // Close the connection 
      if (conn != null) 
      { 
       conn.Close(); 
      } 
     } 
     return ret; 
    } 


public class Experiment 
{ 
    public int m_ExperimentID; 
    public int m_ProjectID; 
    public string m_Name; 
    public string m_Description; 
    public int m_UserID; 
    public DateTime m_DateTime; 
} 
+0

有大量的在線文章如何將MSSQL DATETIME格式化爲DateTime .NET結構。我猜你使用了錯誤的變量類型,並且因爲這個原因他們不能被鑄造。閱讀此:http://stackoverflow.com/questions/1181662/is-there-any-difference-between-datetime-in-c-sharp-and-datetime-in-sql-server –

回答

0

你說:

我在SQL Server中的表中有一個名爲SQL_DATETIME

和列但你使用:

exp.m_DateTime = (DateTime)dsExperiments.Tables["Experiment"] 
             .Rows[0]["DateTime"]; 

註名。我很驚訝你會得到一個投射問題。是否有可能在現實中你的名字是正確的,但你的行不包含任何值,所以你試圖將DBNull.Value投到DateTime

此外,我會說:

  • 沒有必要按引用傳遞exp
  • 我會親自分離異常從數據庫中獲取處理;我讓UI處理異常
  • 我會直接使用命名參數,而不是包括實驗ID進入SQL
+0

喬恩 - 我認爲你是對的。我sql表中的datetime列設置爲允許NULLS,在這種情況下,該值爲NULL。我只是改變它不允許NULLS,爲現有記錄添加值,並且投射問題消失了!我認爲這是一個愚蠢的錯誤。我很好奇你爲什麼說我不需要通過引用傳遞exp ...我需要將exp對象返回給調用代碼。也許我錯過了一些東西。無論如何,謝謝你的優秀評論。 –

+0

@BryanGreenway:閱讀http://pobox.com/~skeet/csharp/parameters.html –

+0

偉大的文章。爲我清理一些東西。再次感謝您的幫助。 –

0

是否使用數據綁定?如果是這樣,你是否綁定到適當的財產? SQL是一個可爲空的類型還是不是?

0

試試這個:

 exp.m_DateTime = DateTime.Parse(dsExperiments.Tables["Experiment"].Rows[0]["DateTime"].ToString()); 
0

的問題可能是包含數據(Tables["Experiment"])DataTable不認爲列["DateTime"]是DateTime類型,而是一個字符串。

我認爲你必須選擇:

  1. 如果您確信它總是一個DateTime,簡單地分析它:

    exp.m_DateTime =DateTime.Parse(dsExperiments.Tables["Experiment"].Rows[0]["DateTime"].ToString()); 
    
  2. 設置dsExperiments.Tables["Experiment"].Columns["DateTime"].DataType=typeof(DateTime);嘗試讀取它之前。

相關問題