从电子表格中读取时,获取OleDbDataReader对象以接受空值

时间:2012-10-03 20:51:03

标签: c# datareader

从excel电子表格中读取时,有没有办法让OleDbDataReader接受空值?我们有以下专栏:

ID | CourseCode | CourseHours |国家| EffectiveDate |到期日期

有时CourseHours列将为空白。这搞砸了我的代码,因为不是读者[2]为空,读者[2] =国家。

这是我现在使用的方法,但似乎有点麻烦。

OleDbDataReader reader;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
    connection.Open();
    reader = command.ExecuteReader();


    while (reader.Read())
    {
        string Id = "";
        string courseCode = "";
        string hours = "";
        string state = "";
        string effectiveDate = "";
        string expirationDate = "";
        string method = "";


        //Add manadatory values
        Id = reader[0].ToString();
        courseCode = reader[1].ToString();

        int value = 0;
        if (int.TryParse(reader[2].ToString(), out value))
        {
            hours = value.ToString();
            state = reader[3].ToString();
            effectiveDate = reader[4].ToString();

            //Check for null in expiration date
            if (!DBNull.Value.Equals(reader[5]))
            {
                expirationDate = reader[5].ToString();
            }
        }
        else
        {
            state = reader[2].ToString();
            effectiveDate = reader[3].ToString();

            //Check for null in expiration date
            if (!DBNull.Value.Equals(reader[5]))
            {
                expirationDate = reader[5].ToString();
            }
        }

0 个答案:

没有答案