一种方法是使用OleDbCommand.ExecuteReader读取找到的每个表的架构。
OleDbConnection con = new OleDbConnection(connectionString); DataSet tablesFromDB = new DataSet(); DataTable schemaTbl; try { // Open the connection con.Open(); object[] objArrRestrict = new object[] { null, null, null, "TABLE" }; // Get the table names from the database we're connected to schemaTbl = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, objArrRestrict); // Not sure if this is correct syntax...fix it if it isn't :) String commandText = @"SELECt * FROM {0}"; // Get each table name that we just found and get the schema for that table. foreach (DataRow row in schemaTbl) { DataTable dt = new DataTable(); OleDbCommand command = new OleDbCommand(String.Format(commandText, row["TABLE_NAME"] as String), con); dt.Load(command.ExecuteReader(CommandBehavior.SchemaOnly)); tablesFromDB.Tables.Add(dt); } }
这样,您可以遍历DataSet的DataTable集合并获取列名称和列字段类型。
foreach (DataTable dt in tablesFromDB){ foreach (DataColumn dc in dt.Columns) { // Do something with the column names and types here // dc.ColumnName is the column name for the current table // dc.DataType.ToString() is the name of the type of data in the column }}
这样做可能是更好的方法,但是我认为这是一个开始。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)