0

有没有办法让 OleDbDataReader 从 Excel 电子表格读取时接受空值?我们有以下列:

身份证 | 课程代码 | 课时 | 状态 | 生效日期 | 截止日期

有时 CourseHours 列会是空白的。这搞砸了我的代码,因为不是 reader[2] 为空,而是 reader[2] = State。

这是我现在使用的方法,但看起来有点麻烦。

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();
            }
        }
4

0 回答 0