C# 獲取數(shù)據(jù)庫(kù)中所有表名、列名的示例代碼
C# 獲取數(shù)據(jù)庫(kù)中所有表名、列名,實(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ù)庫(kù)中所有表名、列名的文章就介紹到這了,更多相關(guān)C# 獲取數(shù)據(jù)庫(kù)表名、列名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)一個(gè)控制臺(tái)的點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)一個(gè)控制臺(tái)的點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
基于C#實(shí)現(xiàn)簡(jiǎn)單的二維碼和條形碼的生成工具
這篇文章主要為大家詳細(xì)介紹了如何基于C#實(shí)現(xiàn)簡(jiǎn)單的二維碼和條形碼工具,用于二維碼條形碼的生成與識(shí)別,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
C# string格式的日期時(shí)間字符串轉(zhuǎn)為DateTime類型的方法
這篇文章主要介紹了C# string格式的日期時(shí)間字符串轉(zhuǎn)為DateTime類型的方法,需要的朋友可以參考下2017-02-02
C#實(shí)現(xiàn)系統(tǒng)托盤通知的方法
這篇文章主要介紹了C#實(shí)現(xiàn)系統(tǒng)托盤通知的方法,涉及C#系統(tǒng)api調(diào)用的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#調(diào)用百度翻譯API實(shí)現(xiàn)一個(gè)翻譯功能
一直喜歡用Google Translate API進(jìn)行在線翻譯,但是服務(wù)越來(lái)越慢這篇文章,所以只能換一個(gè)了,主要給大家介紹了關(guān)于C#調(diào)用百度翻譯API實(shí)現(xiàn)一個(gè)翻譯功能的相關(guān)資料,需要的朋友可以參考下2021-06-06
C#匿名委托和Java匿名局部?jī)?nèi)部類使用方法示例
Java在嵌套類型這里提供的特性比較多,假設(shè):Java的字節(jié)碼只支持靜態(tài)嵌套類,內(nèi)部類、局部?jī)?nèi)部類和匿名局部?jī)?nèi)部類都是編譯器提供的語(yǔ)法糖,這個(gè)假設(shè)目前沒法驗(yàn)證(看不懂字節(jié)碼),本文先來(lái)看一下C#是如何為我們提供的這種語(yǔ)法糖2013-11-11

