2

I just want to to know if some data exists in the database.

Normaly I use SqlDataReader, take a loop SqlDataReader put variable in array or list, and in Business layer again loop over the array or List and compare with the X data to see if it is in the list or array.

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

I think this is not efficient, there are two loops (in Data Access layer to Collect and in Business layer to compare)

Is there another way to do this with a DataSet?

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
user609511
  • 4,091
  • 12
  • 54
  • 86
  • are you talking here about checking if one object(non-list) is in a list of objects or are do you want to know if a list of objects is found in another list of objects? – Răzvan Flavius Panda Oct 05 '11 at 09:09
  • Can you add more detail? Do you mean *any* data, some *specific* data, or a certain number of rows? Or are you talking more generally? – Kirk Broadhurst Oct 05 '11 at 12:09

3 Answers3

1

No there is'nt 'In' or 'Contains' function in DataSet because DataSet itself is a container of DataTable and data is saved in DataRow associated with any particular DataTable.

The simplest method to check if data exists in database on not, is to write an SQL Count statement e.g. SELECT COUNT(columnName) FROM tableName WHERE columnName = 'some value' . If 'sum value' doesn't exist in database it will return 0, return the count otherwise.

Waqas
  • 6,812
  • 2
  • 33
  • 50
0

Basically DataSet is only a container of DataTable(s). If you want to find out about a particular data in DataTable instance inside DataSet instance, you can get DataTable instance from DataSet and there is an instance method called "Select" method (call it with parameter) to query specific data from DataTable instance.

0

i found on internet reference to:

Stack and Find Data

My Bussines Layer:

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

Data Acces layer:

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

So now i only have 1 loop --> just in my Bussines layer, NOT in DAL.

thanks you all

Community
  • 1
  • 1
user609511
  • 4,091
  • 12
  • 54
  • 86