0

我可能做错了,但这里是:

    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [CRPRList$]", XLSConnect);
    //the XLSConnect is from a spreadsheet. I've confirmed the data is there.
    DataSet ds = new DataSet();
    adapter.Fill(ds);
    DataTable thisTable = ds.Tables[0];
    //The table exists and it does populate
    thisTable.Columns.Add("Summary");
    //The new column is added and I am able to populate it (below)

    for (int rowNum = 0; rowNum < thisTable.Rows.Count; rowNum++) { 
        //Here I'm cycling through the row items

        DataRow row = thisTable.Rows[rowNum];
        string pr = row["PR"].ToString();
        string cr = row["CR"].ToString();
        //The first two column values are grabbed and examined

        if ((pr != "" && pr != null)&&(cr == null || cr == "")) {
           //if the first column is empty then the second is examined and I am attempting
           //to populate the first column using the searchForCRNumber method
           //This method returns a string

            cr = this.searchForCRNumber(pr); //assignment works
            row[0] = cr; //Here is the problem, this assignment does not work (more explained below)
        }
        row["Summary"] = this.searchForSummary(cr);
        row.AcceptChanges();
    }
    this.showResults(thisTable);//works, except the changes above are not applied

该行row[0] = cr;自然是一个空值,因此它作为类型出现DBNull并且不接受 cr字符串。

我得到的错误是System.FormatException: Input string was not in a correct format.

我已经搜索了转换row[0]为空字符串的方法或其他一些转换的方法,cr到目前为止我还没有找到一种方法来做到这一点。

编辑 我正在添加更多内容以帮助了解围绕此问题发生的情况。我以为我在上面已经足够彻底了,但也许不是......

数据XLSConnect来自:

private bool XSLConnection(string filePath, string fileType) { // returns false if XSL or XSLX is not the fileType
    string strConn;
    if (fileType.ToLower() == "xls" || fileType.ToLower() == "xlsx") {
        strConn = (fileType == "xlsx") ? string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath) : string.Format("Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0\"", filePath);

        XLSConnect = new OleDbConnection(strConn);
        return true;
    } else return false;
}

这是searchForCRNumber方法:

private string searchForCRNumber(string PRNumber) {
    return this.getProperty("MyReturnColumn", PRNumber, "MyMatchColumn");
}

这是getProperty方法:

private string getProperty(string searchProperty, string searchVal, string rtrnProperty) {
    string undefined = "Undefined";
    string rtrn = null;
    string query = "SELECT " + rtrnProperty + " FROM MySQLTable WHERE " + searchProperty + " = '" + searchVal + "'";
    this.QCConn.Open();

    SqlDataReader reader = this.runQCConnect(query).ExecuteReader();
    reader.Read();
    rtrn = (!reader.HasRows)?
        undefined :
        reader[0].ToString();

    if (rtrn == "" || rtrn == null) rtrn = undefined;
    this.QCConn.Close();


    return rtrn;
}

更多编辑 我仍在研究这个,我开始怀疑我是否还没有发现错误。我发现了问题所在,并且仍在努力解决问题。上传的 xls(x) 可以有空单元格。我发现那些空单元格会导致 DataTable 有空单元格,但它们的类型是 DBNull ......当然,它不能(自然地)被分配一个字符串值。

当我在 try/catch 中应用以下内容时:

throw new Exception("PR = " + pr + " and typeof = " + pr.GetType().ToString() + " && CR = " + cr + " and typeof = " + cr.GetType().ToString() + "\nCell typeof = " + row["CR"].GetType().ToString());

我得到这个回应:

PR = 7800 and typeof = System.String && CR = Undefined and typeof = System.String
Cell typeof = System.DBNull 

如您所见,该单元格是 System.DBNull 并且不接受字符串值。这是我错误的根源。但我仍然不知道如何使这项工作。有什么我想念的吗?无论 DBNull 值如何,如何将字符串值分配给这些单元格?

4

1 回答 1

2

DBNull不是空字符串,将其转换为字符串不会给你null.

如果这是您想要的,您可能应该在将其转换为字符串之前进行检查。执行以下操作:

string pr = (row["PR"] == System.DBNull.Value) ? null : row["PR"].ToString();
string cr = (row["CR"] == System.DBNull.Value) ? null : row["CR"].ToString();
于 2013-03-19T18:38:32.633 回答