-->

改变从的DBNull数据行项目为一个字符串值(Change a datarow item from

2019-10-29 08:53发布

我可能会做这一切错误的,但这里有云:

    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;
}

更多编辑我还在就这一个,我王建宇,山西高等学校想知道如果我还没有找到一个bug。 我发现什么是错的,现在还在研究一种方法来解决它的工作。 被上传的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值的指定字符串值这些细胞?

Answer 1:

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

如果这是你想要什么,你应该将其转换为字符串之前检查这一点。 这样做:

string pr = (row["PR"] == System.DBNull.Value) ? null : row["PR"].ToString();
string cr = (row["CR"] == System.DBNull.Value) ? null : row["CR"].ToString();


文章来源: Change a datarow item from DBNull to a string value