这篇文章主要介绍了C#获取数据库中所有表名、列名,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
C# 获取数据库中所有表名、列名,实现代码如下所示:
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;
}
调用
DBData.getInstance()._tableNames = GetTablesName(OleDbSchemaGuid.Tables);
foreach (var tableName in DBData.getInstance()._tableNames)
{
List<Dictionary<string, string>> tmp = GetColsName(OleDbSchemaGuid.Columns, tableName);
}
通过dataTable获取
/// <summary>
/// 根据datatable获得列名
/// </summary>
/// <param name="dt">表对象</param>
/// <returns>返回结果的数据列数组</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;
}
到此这篇关于C# 获取数据库中所有表名、列名的文章就介绍到这了,更多相关C# 获取数据库表名、列名内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
沃梦达教程
本文标题为:C# 获取数据库中所有表名、列名的示例代码
猜你喜欢
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- .NET CORE DI 依赖注入 2023-09-27
- 如何使用C# 捕获进程输出 2023-03-10
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- Oracle中for循环的使用方法 2023-07-04
- user32.dll 函数说明小结 2022-12-26
- Unity Shader实现模糊效果 2023-04-27
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- c# 模拟线性回归的示例 2023-03-14
- Unity3D实现渐变颜色效果 2023-01-16