C# 獲取數(shù)據(jù)庫中所有表名、列名的示例代碼
C# 獲取數(shù)據(jù)庫中所有表名、列名,實(shí)現(xiàn)代碼如下所示:
List<Dictionary<string, string>> GetColsName(Guid gtype,string tableName,string itemIndex= "COLUMN_NAME") { DataTable dsTablesData = DbDataHelper.GetCon().GetOleDbSchemaTable(gtype, new Object[] { null, null, tableName, null }); List<Dictionary<string, string>> ditCol = new List<Dictionary<string, string>>() ; for (int i = 0; i < dsTablesData.DefaultView.Table.Rows.Count; i++) { ditCol.Add(new Dictionary<string, string> { { i.ToString(), i.ToString() } }); } foreach (DataRow item in dsTablesData.DefaultView.Table.Rows) { int pos = Convert.ToInt32(item["ORDINAL_POSITION"]); int typeIndex = Convert.ToInt32(item["DATA_TYPE"]); ditCol[pos-1]= new Dictionary<string, string> { { item[itemIndex].ToString(), DBData.getInstance().GetColNameType(typeIndex) } }; } return ditCol; } List<string> GetTablesName(Guid gtype,string tableType ="TABLE", string strTableName =null , string itemIndex= "TABLE_NAME") { List<string> strNames = new List<string>(); DataTable dsTablesData = DbDataHelper.GetCon().GetOleDbSchemaTable(gtype, new Object[] { null, null, strTableName, tableType }); foreach (DataRow item in dsTablesData.DefaultView.Table.Rows) { strNames.Add(item[itemIndex].ToString()); } return strNames; }
調(diào)用
DBData.getInstance()._tableNames = GetTablesName(OleDbSchemaGuid.Tables); foreach (var tableName in DBData.getInstance()._tableNames) { List<Dictionary<string, string>> tmp = GetColsName(OleDbSchemaGuid.Columns, tableName); }
通過dataTable獲取
/// <summary> /// 根據(jù)datatable獲得列名 /// </summary> /// <param name="dt">表對(duì)象</param> /// <returns>返回結(jié)果的數(shù)據(jù)列數(shù)組</returns> public static List<string> GetColumnsByDataTable(DataTable dt) { List<string> list = new List<string>(); foreach (DataColumn item in dt.Columns) { list.Add(item.ColumnName); } return list; }
到此這篇關(guān)于C# 獲取數(shù)據(jù)庫中所有表名、列名的文章就介紹到這了,更多相關(guān)C# 獲取數(shù)據(jù)庫表名、列名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#生成excel示例sql數(shù)據(jù)庫導(dǎo)出excel
這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導(dǎo)出數(shù)據(jù)到excel,大家參考使用吧2014-01-01數(shù)字金額大寫轉(zhuǎn)換器制作代碼分享(人民幣大寫轉(zhuǎn)換)
一個(gè)人民幣大寫的擴(kuò)展方法,可以做成數(shù)字金額大寫轉(zhuǎn)換器,大家參考使用吧2013-12-12C# JavaScriptSerializer序列化時(shí)的時(shí)間處理詳解
這篇文章主要為大家詳細(xì)介紹了C# JavaScriptSerializer序列化時(shí)的時(shí)間處理詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08C#中將DataTable轉(zhuǎn)換成CSV文件的方法
DataTable用于在.net項(xiàng)目中,用于緩存數(shù)據(jù),DataTable表示內(nèi)存中數(shù)據(jù)的一個(gè)表,在.net項(xiàng)目中運(yùn)用C#將DataTable轉(zhuǎn)化為CSV文件,接下來通過本文給大家提供一個(gè)通用的方法,感興趣的朋友可以參考下2016-10-10