问题描述
我在数据库中有一个按以下格式自定义的列:
I have a column in a database that is customized in the following format:
convert(varchar, PERSON.DateOfBirth, 103) as DateOfBirth
我正在尝试使用数据访问层中的列表和数据读取器在网格视图中显示一个表(不是来自表示层)并且问题存在于字段中,其中DateOfBirth的值在类和数据库中被声明为DateTime。
我正在尝试从数据库中读取此字段,如下所示:
I'm trying to show a table in Grid View using a list and a data reader from Data Access Layer (not from presentation layer) and the problem exists by field where the value of DateOfBirth is declared as DateTime in a class and a database.
I'm trying to read this field from database as following:
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
Person person= new Person();
person.ID_Person = reader.GetInt32(0);
//here are more rows
person.DateOfBirth = reader.GetDateTime(4);
//and here are some more rows
ListPerson.Add(person);
}
}
reader.Close();
我试图读取自定义日期时间的行,它会抛出一个错误,无法读取该类型的日期。
任何人都可以帮我解决这个问题并从数据库中读取日期格式。
提前谢谢你的回复。
干杯。
The row where I'm trying to read customized datetime, it throws me an error and can not read that type of Date.
Can anyone help me to fix this and read the date format from database.
Thank you in advance for your reply.
Cheers.
推荐答案
这篇关于使用DataReader在DB中自定义DateTime fromat时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!