【问题标题】:Dataset returning ZERO results返回零结果的数据集
【发布时间】:2010-10-29 09:04:42
【问题描述】:

我正在尝试从数据库中填写我的 winform 应用程序的组合框。我知道数据库中有信息。我知道SP有效。它返回正确的列名。但是DataSet本身是空的吗?救命!?!?

从我的表单调用-->

cboDiagnosisDescription.Properties.DataSource = myDiagnosis.RetrieveDiagnosisCodes();

RetrieveDiagnosisCodes -->

public DataSet RetrieveDiagnosisCodes()
    {
        string tableName = "tblDiagnosisCues";
        string strSQL = null;
        DataSet ds = new DataSet(tableName);
        SqlConnection cnn = new SqlConnection(Settings.Default.CMOSQLConn);
        //strSQL = "select * from " & tableName & " where effectivedate <= getdate() and (termdate >= getdate() or termdate is null)"
        strSQL = "select tblDiagnosisCues.*, tblDiagnosisCategory.Description as CategoryDesc, tblDiagnosisSubCategory.Description as SubCategoryDesc " + "FROM dbo.tblDiagnosisCategory INNER JOIN " + "dbo.tblDiagnosisSubCategory ON dbo.tblDiagnosisCategory.Category = dbo.tblDiagnosisSubCategory.Category INNER JOIN " + "dbo.tblDiagnosisCues ON dbo.tblDiagnosisSubCategory.SubCategory = dbo.tblDiagnosisCues.SubCategoryID " + "where effectivedate <= getdate() and (termdate >= getdate() or termdate is null) order by tblDiagnosisCues.Description";
        SqlCommand cmd = new SqlCommand(strSQL, cnn) {CommandType = CommandType.Text};
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        try
        {
            //cnn.Open();
            da.Fill(ds);
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
            cmd.Dispose();
            da.Dispose();
            //ds.Dispose();
            cnn.Close();
            cnn.Dispose();
        }
        return ds;
    }

我知道它返回正确列名的原因是我使用 DevExpress LookUpEdit 框尝试了以下操作,它从数据库中填充了正确的列 -->

            var myDiagnosis = new Diagnosis();
        var ds = myDiagnosis.RetrieveDiagnosisCodes();
        lkuDiagnosis.Properties.DataSource = ds;
        lkuDiagnosis.Properties.PopulateColumns();
        lkuDiagnosis.Properties.DisplayMember = ds.Tables[0].Columns[1].ColumnName;
        lkuDiagnosis.Properties.ValueMember = ds.Tables[0].Columns[0].ColumnName;

想法?主要是,我什至不知道如何继续跟踪这个......如何调试它?

编辑 1

根据评论,我自己运行了以下 SQL,它返回了 650 个结果...

select tblDiagnosisCues.*, 
    tblDiagnosisCategory.Description as CategoryDesc,   
    tblDiagnosisSubCategory.Description as SubCategoryDesc 
FROM dbo.tblDiagnosisCategory 
    INNER JOIN dbo.tblDiagnosisSubCategory 
    ON dbo.tblDiagnosisCategory.Category = dbo.tblDiagnosisSubCategory.Category 
        INNER JOIN dbo.tblDiagnosisCues ON dbo.tblDiagnosisSubCategory.SubCategory = dbo.tblDiagnosisCues.SubCategoryID 
where effectivedate <= getdate() and (termdate >= getdate() or termdate is null) order by tblDiagnosisCues.Description

【问题讨论】:

  • 以交互方式尝试 SELECT 查询,例如使用 osql - 该查询有很多条件,也许你表达得很糟糕,结果是 0 行;如果是这样,那么osql 和类似的工具可以让您查看当您削弱某些条件时会发生什么,等等。

标签: c# sql winforms debugging data-access-layer


【解决方案1】:

//cnn.open();

...

//ds.dispose();

无需在数据集构造函数中指定表名。 fill 方法将添加一个表格。也无需打开连接,因为 sqldataadapter 将为您打开和关闭连接。另外,我更喜欢返回一个数据表,而不是一个表的数据集。

代码可以重构为以下...当然,如果要记录异常,请添加 try catch。

public DataTable RetrieveDiagnosisCodes()
{
    //string tableName = "tblDiagnosisCues";
    DataSet ds = new DataSet();
    Datatable dt = null;
    //strSQL = "select * from " & tableName & " where effectivedate <= getdate() and (termdate >= getdate() or termdate is null)"
    string strSQL = "select tblDiagnosisCues.*, tblDiagnosisCategory.Description as CategoryDesc, tblDiagnosisSubCategory.Description as SubCategoryDesc " + "FROM dbo.tblDiagnosisCategory INNER JOIN " + "dbo.tblDiagnosisSubCategory ON dbo.tblDiagnosisCategory.Category = dbo.tblDiagnosisSubCategory.Category INNER JOIN " + "dbo.tblDiagnosisCues ON dbo.tblDiagnosisSubCategory.SubCategory = dbo.tblDiagnosisCues.SubCategoryID " + "where effectivedate <= getdate() and (termdate >= getdate() or termdate is null) order by tblDiagnosisCues.Description";

    using(SqlDataAdapter da = new SqlDataAdapter(strSQL, Settings.Default.CMOSQLConn))
    {
      da.Fill(ds);
    }
    if (ds.Tables.Count > 0)
    {
      dt = ds.Tables[0];
    }

    return dt;
}

【讨论】:

  • 这些是最佳实践的一般提示吗?或可能的解决方案?谢谢
  • 我认为这两个建议都不能被视为“最佳实践”。两者都可以被视为开发者偏好。如果您将其序列化为 XML,则 Dataset 类的构造函数中的 Dataset 名称指定 Root 元素。 (而不是默认的“NewDataset”)
  • @Mr_Mom 是的。再次查看代码,无论成功或失败,finally 块都会执行。 ds.dispose 命令有效地清除了您刚刚下载的所有数据。我同意返回数据表将是更好的选择。只需将“return ds”更改为“return ds.Tables[0]”
  • 我认为您可以使用数据集...但是您需要将值成员和显示成员设置为“tablename.columnname”,而不仅仅是“columnname”。通过使用数据表,您只需节省绑定,无需在数据集中查找表。
  • DataSet 是表、关系、键等的集合。它很像一个可移植的数据库,所有设置都是为了使用方便的“断开连接”数据模型。 .NET 框架中的大多数对象在默认情况下无法数据绑定到如此复杂的对象。然而,DataTable 是一个单一实体(尽管有列和行),大多数对象都知道如何轻松绑定,您只需指定要引用的列。
【解决方案2】:

如果数据正确绑定到另一个控件,则表明数据绑定过程存在问题。对于有问题的组合框,您的数据绑定设置是什么样的?所有列名的拼写和设置是否正确?

【讨论】:

  • 我没有进行数据绑定... :( 我只是设置了数据源,然后指定了显示和值成员。这是我的问题吗?
  • 不,设置数据源是“数据绑定”过程。无需像在 asp.net 中那样显式调用数据绑定方法,但听起来您正在寻找正确的解决方案。
猜你喜欢
  • 2011-08-07
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多