C# 将datarow项从DBNull更改为字符串值

C# 将datarow项从DBNull更改为字符串值,c#,asp.net,datatable,datarow,C#,Asp.net,Datatable,Datarow,我可能做错了这一切,但这里有: 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); Data

我可能做错了这一切,但这里有:

    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
以下是
searchForCRNumber
方法:

private string searchForCRNumber(string PRNumber) {
    return this.getProperty("MyReturnColumn", PRNumber, "MyMatchColumn");
}
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;
}
下面是
getProperty
方法:

private string searchForCRNumber(string PRNumber) {
    return this.getProperty("MyReturnColumn", PRNumber, "MyMatchColumn");
}
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值?

DBNull
不是空字符串,将其转换为字符串不会为您提供
null

如果这是您想要的,您可能应该在将其转换为字符串之前检查它。做一些类似于:

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

您可以发布searchForCRNumber(pr)过程吗?
行[0]=cr这里不是问题所在。这不会导致该异常。但是,我可以确定它是否会按预期返回字符串。这一点应该没有意义。行[0]显示为type
DBNull
,而cr显示为type
string
。这里是调试显示problem@Jimmmy:但是,您不是在返回索引属性,而是在设置它。这可能会导致但不会导致
格式异常
。这仍然不会更改行[0]的DBNull类型和错误persists@Jimmmy,我怀疑您对
行[0]
的定义不允许空值;告诉我们
行.Table.Columns[0]
的属性是什么。好的,也许这是正确的方向。我不熟悉使用
数据集
,我给它的所有属性都已经显示出来了。唯一需要补充的是,数据来自一个电子表格,其中可能有空单元格。@Jimmmy告诉我们
此表的所有列的所有属性。没有这些信息,我们无法帮助您。可能出现的问题数量巨大(列数错误、值无效、值不一致、数据类型冲突、键冲突、约束冲突、触发器等等),所有这些都在原始帖子中。我想你可以说我把一切都忘了(??)。