【问题标题】:Does DataSet have a Contain or In functionality?DataSet 是否具有包含或输入功能?
【发布时间】:2011-12-01 06:17:29
【问题描述】:

我只是想知道数据库中是否存在某些数据。

我通常使用SqlDataReader,使用循环SqlDataReader将变量放入数组或列表中, 并在业务层再次循环遍历数组或列表并与 X 数据进行比较以查看它是否在列表或数组中。

 SqlDataReader readerOne = comm_SelectOne.ExecuteReader();
 while (readerOne.Read())
 { 
    ... 
 }

我觉得这样效率不高,有两个循环(Data Access layer to Collect和Business layer to compare)

还有其他方法可以用 DataSet 做到这一点吗?

【问题讨论】:

  • 您是在谈论检查一个对象(非列表)是否在对象列表中,还是您想知道是否在另一个对象列表中找到了对象列表?跨度>
  • 你能补充更多细节吗?您是指任何数据、一些特定数据还是特定数量的行?还是说的更笼统?

标签: c# asp.net dataset data-access-layer contains


【解决方案1】:

DataSet 中没有'In' 或'Contains' 函数,因为DataSet 本身是DataTable 的容器,并且数据保存在与任何特定DataTable 关联的DataRow 中。

检查数据库中是否存在数据的最简单方法是编写 SQL Count 语句,例如SELECT COUNT(columnName) FROM tableName WHERE columnName = 'some value' 。如果数据库中不存在“总和值”,它将返回 0,否则返回计数。

【讨论】:

    【解决方案2】:

    基本上,DataSet 只是 DataTable(s) 的容器。如果您想在DataSet 实例中查找DataTable 实例中的特定数据,您可以从DataSet 中获取DataTable 实例,并且有一个名为“Select”方法的实例方法(使用参数调用它)可以从DataTable 实例中查询特定数据。

    【讨论】:

      【解决方案3】:

      我在互联网上找到参考:

      StackFind Data

      我的业务层:

       public List<string> CompareInsee(string TheZone, List<object> InseList)
          {
              try
              {
                  List<string> TempDict = new List<string>();
                  RempliClientInseExtracted(TheZone, ref NomTable);
                  DataTable TempDS = oClInse.Get_All_Inse(NomTable);
                  DataRow drFound;
                  DataColumn[] dcPk = new DataColumn[1];             
      
                  // Set Primary Key
                  dcPk[0] = TempDS.Columns["NO_INSEE"];
                  TempDS.PrimaryKey = dcPk;
                  // Find the Row specified in txtFindArg
      
                 foreach (var oItem in InseList)
                 {
                     drFound = TempDS.Rows.Find(oItem);
                     if (drFound != null) TempDict.Add( oItem.ToString()); 
                 }
                 return TempDict;
      
              }
              catch (Exception excThrown)
              {
                  if (!excThrown.Message.StartsWith("Err_")) { throw new Exception("Err_BL_ReadAllClientInsee", excThrown); }
                  else { throw new Exception(excThrown.Message, excThrown); }
              }
          }
      

      数据访问层:

       public DataTable Get_All_Inse(string NomTable)
          {
              try
              {
                  using (var connectionWrapper = new Connexion())
                  {
                      var connectedConnection = connectionWrapper.GetConnected();
                      string sql_SelectAll = "SELECT * FROM " + NomTable;
                      SqlCommand comm_SelectAll = new SqlCommand(sql_SelectAll, connectionWrapper.conn);
      
                      SqlDataAdapter adapt_SelectAll = new SqlDataAdapter();
                      adapt_SelectAll.SelectCommand = comm_SelectAll;
                      DataTable dSet_SelectAll = new DataTable();
                      adapt_SelectAll.Fill(dSet_SelectAll);
                      dSet_SelectAll.Dispose();
                      adapt_SelectAll.Dispose();
                      return dSet_SelectAll;
                  }
              }
              catch (Exception excThrown)
              {
                  if (!excThrown.Message.StartsWith("Err_")) { throw new Exception("Err_GetAllUsrClient", excThrown); }
                  else { throw new Exception(excThrown.Message, excThrown); }
              }
          }
      

      所以现在我只有 1 个循环 --> 只是在我的 Bussines 层中,而不是在 DAL 中。

      谢谢大家

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        • 2012-10-07
        相关资源
        最近更新 更多