数据库
首页 > 数据库> > 使用SqlDataReader的DateTime2到C#DateTime

使用SqlDataReader的DateTime2到C#DateTime

作者:互联网

我意识到这可能是一个骗局,但我花了几个小时寻找答案,似乎无法找到答案.

我目前正在创建一个检索Concert数据的Web API.

我有一个SQL Server表,其中包含一个开始和结束日期,两者都作为datetime2类型.我已经以这种格式插入了日期,它们在查看数据库时不会出现任何问题:

2015-10-08T20:00:00.0000000+01:00

我的模特:

public class Concert
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int LocationId { get; set; }

    [Column(TypeName = "DateTime2")]
    public DateTime Start { get; set; }

    [Column(TypeName = "DateTime2")]
    public DateTime End { get; set; }

    public string Description { get; set; }
    public string Url { get; set; }
}

我的类中的方法会调出我的数据库数据:

    public List<Concert> getAll() 
    {
        List<Concert> concerts = new List<Concert>();

        SqlConnection connection = CasWebAPIdb.getConnection();
        String selectAll = "SELECT ConcertId, ConcertName, ConcertLocationId FROM dbo.Concerts";
        SqlCommand selectCommand = new SqlCommand(selectAll, connection);

        try
        {
            connection.Open();
            SqlDataReader reader = selectCommand.ExecuteReader();
            var isoDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;

            while (reader.Read())
            {
                //Debug.WriteLine("lol: " + reader["ConcertEnd"].GetType());

                Concert concert = new Concert();

                concert.Id = (int)reader["ConcertId"];
                concert.Name = reader["ConcertName"].ToString();
                concert.LocationId = (int)reader["ConcertLocationId"];
                concert.Start = (DateTime)reader["ConcertStart"];
                concert.End = (DateTime)reader["ConcertEnd"];

                concerts.Add(concert);
            }
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }
        return concerts;
    }
}

调试时出现此错误:

An exception of type ‘System.IndexOutOfRangeException’ occurred in System.Data.dll but was not handled in user code

我已经尝试了很多东西并且遵循了很多示例和代码,但我似乎无法正确地转换它.有人有想法吗?

我忘了在我的查询中添加’concertStart’和’concertEnd’.
问题解决了,谢谢!

解决方法:

首先,如果你想读取ConcertStart和ConcertEnd的值,你必须将它们包含在SELECT !! $中

string selectAll = @"SELECT ConcertId, ConcertName, ConcertLocationId,
                            ConcertStart, ConcertEnd     <<--- add these!! 
                     FROM dbo.Concerts";

试试这个:

while (reader.Read())
{
    Concert concert = new Concert();

    concert.Id = (int)reader["ConcertId"];
    concert.Name = reader["ConcertName"].ToString();
    concert.LocationId = (int)reader["ConcertLocationId"];
    concert.Start = reader.GetFieldValue<DateTime>(reader.GetOrdinal("ConcertStart"));
    concert.End = reader.GetFieldValue<DateTime>(reader.GetOrdinal("ConcertEnd"));

    concerts.Add(concert);
}

我毫不费力地从SQL Server数据库中读取DATETIME2(3)值

reader.GetFieldValue<DateTime>(reader.GetOrdinal("ConcertEnd"));

那对你有用吗?

标签:c,datetime,sql-server,sqldatareader,datetime2
来源: https://codeday.me/bug/20190528/1171327.html