C# Tips and Code
Home C# Tips and Code Linux Webmaster David's WEBDEX

Want to find all the tables in a database. Here's a code fragment from a program that gets all the table names.  TableNameList is a public variable of type ArrayList in the containing class.

public ArrayList GetTables(OleDbConnection conn)

{    	
	string scr;
	DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
			new object[] {null, null, null, "TABLE"});

	TableNameList = new ArrayList();
	foreach(DataRow myRow in schemaTable.Rows)
	{
		scr = string.Format("{0}",myRow["TABLE_NAME"]);
		TableNameList.Add(scr);
	}
	return TableNameList;
}

After calling the above routine to fill in the global variable TableNameList, the following routine will read all the tables into a dataset called DataSetBnB. I've commented out some code that writes status data to the screen.

private void ReadDBIntoDataSet()
{
    //string scr;
    foreach( string TName in this.TableNameList)
    {
        //scr=string.Format("Loading Table: {0}",TName);
	//RB.wasc(scr);
        string selstr=string.Format("SELECT * FROM {0}",TName);
        DataAdapterBnB.SelectCommand.CommandText=selstr;
        DataAdapterBnB.TableMappings.Add(TName,TName);
        DataAdapterBnB.FillSchema(DataSetBnB, SchemaType.Source, TName);
        DataAdapterBnB.Fill(DataSetBnB,TName);
        //scr=string.Format(" - table has {0} rows\n",DataSetBnB.Tables[TName].Rows.Count);
        //RB.wasc(scr);
    }
} 
 

Copyright (c) 2002 by J. David Ball. All rights reserved.