【问题标题】:Change a datarow item from DBNull to a string value将数据行项从 DBNull 更改为字符串值
【发布时间】:2013-03-19 18:31:35
【问题描述】:

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

    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 值如何,如何将字符串值分配给这些单元格?

【问题讨论】:

  • 你能发布 searchForCRNumber(pr) 过程吗?
  • row[0] = cr; 这不是问题所在。这不会导致该异常。
  • 我可以,但是,它确实按预期返回了一个字符串。这一点应该没有实际意义。
  • row[0] 以 DBNull 类型出现,而 cr 以 string 类型出现。这是调试显示问题的地方
  • @Jimmmy:但是,您并没有返回索引属性,而是在设置它。这可能会导致several exceptions,但不会导致FormatException

标签: c# asp.net datatable datarow


【解决方案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();

【讨论】:

  • 这仍然没有改变 row[0] 的类型 DBNull 并且错误仍然存​​在
  • @Jimmmy,我怀疑您对row[0] 的定义不允许空值;告诉我们row.Table.Columns[0] 的属性是什么。
  • 好吧,也许这是正确的方向。我是使用DataSet 的新手,我给它的所有属性都已经显示出来了。唯一要添加的另一件事是数据来自电子表格,其中可能存在空单元格。
  • @Jimmmy 告诉我们thisTable 的所有列的所有属性。如果没有这些信息,我们将无法为您提供帮助。可能的问题数量巨大(列数错误、无效值、非唯一值、冲突的数据类型、键违规、约束违规、触发器等等......)
  • 都在原帖中。我想你可以说我已经把所有东西都保留了(??)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多