[英]Retrieving a date/time value from a Microsoft Access Database
我有一個方法從表中的特定列檢索所有數據。 現在這個方法在數據庫中的字符串是“字符串”或“int”(通過更改為GetInt32)格式時有效,雖然它不喜歡日期字段,我的方法如下:
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);
}
}
此行拋出異常:
timeList.Add(aReader.GetString(0));
錯誤:
指定演員表無效。
列中日期/字段的示例是:
02/05/2012 15:52:45
嘗試
timeList.Add(aReader.GetDateTime(0).ToString());
采用
timeList.Add(DateTime.Parse(aReader.GetString(0)).ToString(yourdateformat));
你的日期格式可以是"dd/mm/yyyy hh:MM:ss"
或者你想要的任何東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.