从数据库中读取SQLite DateTime值并将其分配给C#字符串变量

问题描述:

我有一个数据库,其中包含一个包含DateTime列的数据表。使用SQL Server时,我可以使用下面的代码从数据库中读取的DateTime值:从数据库中读取SQLite DateTime值并将其分配给C#字符串变量

SqlCommand getdate = new SqlCommand("SELECT * FROM EMPinfo WHERE id = @employeeId", connect); 
       getdate.Parameters.AddWithValue("@employeeId", listViewEmployee.SelectedItems[0].SubItems[2].Text); 
       getdate.Connection = connect; 
       connect.Open(); 
       SqlDataReader readList = getdate.ExecuteReader(CommandBehavior.CloseConnection); 
       while (readList.Read()) 
       { 
        lblEmpDob.Text = ((DateTime)readList["dob"]).ToString("d"); 
       } 

更改代码后使用SQLite运行:

SQLiteConnection connect = new SQLiteConnection(@"Data Source=quotevodata.db;"); 
       SQLiteCommand getlistname = new SQLiteCommand("SELECT * FROM EMPinfo WHERE id = @employeeId", connect); 
       getlistname.Parameters.AddWithValue("@employeeId", listViewEmployee.SelectedItems[0].SubItems[2].Text); 
       getlistname.Connection = connect; 
       connect.Open(); 
       SQLiteDataReader readList = getlistname.ExecuteReader(CommandBehavior.CloseConnection); 
       while (readList.Read()) 
       { 
        lblEmpDob.Text = ((DateTime)readList["dob"]).ToString("d"); 

       } 

我不断收到以下错误:“字符串未被识别为有效的日期时间。“

我试过不同的组合和变量的声明,但它不工作。从SQLite数据库中读取DateTime值的正确配置是什么?

+0

您是否尝试过使用'Convert.ToDateTime(readList [“dob”])'而不是简单的转换? – erikscandola

+0

@erikscandola你发布的内容*是“简单演员”,即默认转换为“DateTime”。 –

感谢您的答案,我终于得到它通过改变INSERT语句SQLite的格式工作的建议:

string empDob = dateDOB.Value.ToString("yyyy-MM-dd"); 
    //I then inserted this string into the database with the column configured as a "DATE" datatype. 

前前后后呃,我用下面的语句来读取和格式化日期可用的字符串,并将其优美的工作:

DateTime dateOfBirthEmp = DateTime.Parse(readList["dob"].ToString()); 

    lblEmpDob.Text = dateOfBirthEmp.ToString("d"); 

我真的很感激帮助。

SQLite没有内置的DateTime对象,而是将它们存储为Text,Real或Int值。

从你的错误,你可以推断它是作为文本输出;根据SQLite文档应该采用“YYYY-MM-DD HH:MM:SS.SSS”的格式。

有多种方法可以将此解析为DateTime对象,但我将使用RegEx:

public static DateTime ConvertToDateTime(string str) 
{ 
    string pattern = @"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d{3})"; 
    if (Regex.IsMatch(str, pattern)) 
    { 
     Match match = Regex.Match(str, pattern); 
     int year = Convert.ToInt32(match.Groups[1].Value); 
     int month = Convert.ToInt32(match.Groups[2].Value); 
     int day = Convert.ToInt32(match.Groups[3].Value); 
     int hour = Convert.ToInt32(match.Groups[4].Value); 
     int minute = Convert.ToInt32(match.Groups[5].Value); 
     int second = Convert.ToInt32(match.Groups[6].Value); 
     int millisecond = Convert.ToInt32(match.Groups[7].Value); 
     return new DateTime(year, month, day, hour, minute, second, millisecond); 
    } 
    else 
    { 
     throw new Exception("Unable to parse."); 
    } 
} 

文档:http://www.sqlite.org/datatype3.html

+1

感谢您的建议,但这似乎将字符串格式的日期转换为DateTime格式,这不是我想要完成的。我想从数据库中读取一个日期,它以DateTime格式完美保存到数据库中,并使用字符串格式的标签显示它。 – pallMallchemist

+0

@pallMallchemist数据库返回的对象显然是一个字符串。尝试添加此函数并将代码行更改为lblEmpDob.Text = ConvertToDateTime(readList [“dob”])。ToString(“d”); – JonBee

为什么要转换2次?

如果你在SQLite中有一个Date列,那么提供者可以为你管理它。 您可以直接插入为DateTime并读取为DateTime。

+0

由于某些原因,直接读取日期并将其分配给标签不允许我使用“.ToString(”d“)”功能来格式化日期(缺少更好的单词,它只能在阅读首先是DataeTime格式 – pallMallchemist

+0

在你的解决方案中,你有'dateDOB'(这可能是一个Nullable日期),你不需要转换成一个字符串befor insert,'readList [“dob”]'是一个日期(或可为空日期如果列是可空的)。为什么不使用DateTime?dateOfBirthEmp = readList [“dob”]作为DateTime?;' – PinBack