栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

需要找到数据库列的类型

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

需要找到数据库列的类型

一种方法是使用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    }}

这样做可能是更好的方法,但是我认为这是一个开始。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号