2012-05-03 23 views
0

テーブルの特定の列からすべてのデータを取得する方法があります。Microsoft Accessデータベースから日付/時刻値を取得する

public static void getDates() 
{ 
    //create the database connection 
    OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\ev_mgr.mdb"); 

    //create the command object and store the sql query 
    OleDbCommand actorCommand = new OleDbCommand("select Issue_Time2 from Events", aConnection); 
    try 
    { 
     aConnection.Open(); 

     //create the datareader object to connect to table 
     OleDbDataReader aReader = actorCommand.ExecuteReader(); 

     //Iterate throuth the database 
     while (aReader.Read()) 
     { 
      timeList.Add(aReader.GetString(0)); // Error occurs here 

     } 

     //close the reader 
     aReader.Close(); 

     //close the connection Its important. 
     aConnection.Close(); 
    } 

    //Some usual exception handling 
    catch (OleDbException e) 
    { 
     Console.WriteLine("Error: {0}", e.Errors[0].Message); 
    } 


    foreach (string time in timeList) 
    { 
     Console.WriteLine(time); 
    } 
} 

アンを:フィールドは、データベース内の形式(GetInt32に変更することで)「文字列」または「INT」にあるとき、それは日付フィールドが好きではありませんが、今、このメソッドは、作品次のように、私の方法があります例外は、この行にスローされます。

timeList.Add(aReader.GetString(0)); 

エラー:

Specified cast is not valid.

日付/フィールドの例の列には、次のとおりです。

02/05/2012 15:52:45 

答えて

2

timeList.Add(aReader.GetDateTime(0).ToString()); 
+0

をしたい

timeList.Add(DateTime.Parse(aReader.GetString(0)).ToString(yourdateformat)); 

を試してみてください。 [Microsoft] [1] [1]:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader_methods%28v=vs.71%29.aspx –

+0

ありがとう、私は何か見落とした、それは素晴らしい作品、ありがとう! –

0

使用yourdateformatは"dd/mm/yyyy hh:MM:ss"することができ、またはそれはそこにあることを仮定何でも

+0

こんにちは、ありがとう、それは動作しませんでした:(私はまだ同じエラーが –

関連する問題