【发布时间】:2017-07-26 14:33:06
【问题描述】:
我有一个带有数据表的数据库,其中包括 DateTime 列等。使用 SQL Server 时,我可以使用以下代码从数据库中读取 DateTime 值:
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 值的正确配置是什么?
【问题讨论】:
-
你试过用
Convert.ToDateTime(readList["dob"])代替简单的演员表吗? -
@erikscandola 您发布的是“简单转换”,即默认转换为
DateTime。