Share via


how to check dataset have a specified table is exist or not

Question

Wednesday, October 21, 2009 6:59 PM

hi everyone
 
please i want to know how to make sure if a table exist into a dataset or not

i need code describe what i need like that

is mydataset.tables("tablename").isexist = true then
.........
end if

if you have any idea pleas tell me
thanks a lot

All replies (2)

Thursday, October 22, 2009 7:20 AM âś…Answered

Well, there already is a method to do this; Contains()
But there isn't a property afaik.

            DataSet ds = new DataSet("DS");
            DataTable dtA = new DataTable("A");
            DataTable dtB = new DataTable("B");

            ds.Tables.Add(dtA);
            ds.Tables.Add(dtB);

            Console.WriteLine("DataTable B exists: {0}", ds.Tables.Contains("B")); // True
            Console.WriteLine("DataTable C exists: {0}", ds.Tables.Contains("C")); // False

HTH
//Michael
This posting is provided "AS IS" with no warranties.


Wednesday, October 21, 2009 8:00 PM

Write your own method:

public static bool TableExist(DataSet dataSet, string tableName)
{
     bool Exists = false;

    foreach (DataTable table in dataSet.Tables)
   {
        if (table. TableName == tableName)
        {
              Exists = true;
              break;
         }
    }

    return Exists;
}