2011-11-10 32 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);尝试读取它之前。

相关问题