广杰博客

  • 首页
  • IT资讯
  • IT运维
  1. 首页
  2. IT运维
  3. 正文

C#中如何获取数据库中表的信息和列的信息

04/01/2011 1066点热度 0人点赞 0条评论

获取表的信息:

conn.Open();

string[] restrictions = new string[4];

restrictions[1] = "dbo";

DataTable table = conn.GetSchema("Tables", restrictions);

conn.Close();

返回的table是表的所有信息,而不仅仅是名字,可以通过如下语句查看这些信息:

foreach (System.Data.DataRow row in table.Rows)

{

foreach (System.Data.DataColumn col in table.Columns)

{

Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);

}

}

要获取指定表的信息,关键是要设置数组restrictions的值。对于表而言,这个数组有如下的含义:

Restriction[0]表示表所在的Catalog

Restriction[1]表示表的所有者

Restriction[2]表示表的名字

Restriction[3]表示表的类型:

上面的例子就获取了所有dbo拥有的表的信息。如果要获取所有的用户表,而非系统表,可用如下语句:

conn.Open();

string[] restrictions = new string[4];

restrictions[3] = “BASE TABLE";

DataTable table = conn.GetSchema("Tables", restrictions);

conn.Close();

获取列的信息:

conn.Open();

string[] restrictions = new string[4];

restrictions[1] = "dbo";

DataTable table = conn.GetSchema("Columns", restrictions);

conn.Close();

与获取表的代码很类似,只是GetSchema的第一个参数不同。同样,返回结果取决于restriction的值。此时,

Restriction[0]表示列所在的Catalog

Restriction[1]表示列的所有者

Restriction[2]表示列所在的表的名字

Restriction[3]表示列名

例如:

// restriction string array

string[] res = new string[4];

// dbo拥有的所有表的所有列的信息

res[1] = "dbo";

DataTable t1 = conn.GetSchema("Columns", res);

// 任意owner/schema所拥有的一个叫authors的表的列信息

res[2] = "authors";

DataTable t2 = conn.GetSchema("Columns", res);

//任意owner/schema所拥有的一个叫authors的表的列name的信息

res[2] = "authors"; res[3] = "name ";

DataTable t3 = conn.GetSchema("Columns", res);

//任意owner/schema任意表中的一个列名是name的列的信息。

res[3] = "name";

DataTable t4 = conn.GetSchema("Columns", res);

获取数据库的其它信息都可以使用GetSchema,只是第一个参数不同。这个参数在不同的数据库有差异:

1、在SQL Server中,可以获取的架构集合如下:

· Databases

· ForeignKeys

· Indexes

· IndexColumns

· Procedures

· ProcedureParameters

· Tables

· Columns

· Users

· Views

· ViewColumns

· UserDefinedTypes

2、在Oracle中,可以获取的架构集合如下:

· Columns

· Indexes

· IndexColumns

· Procedures

· Sequences

· Synonyms

· Tables

· Users

· Views

· Functions

· Packages

· PackageBodies

· Arguments

· UniqueKeys

· PrimaryKeys

· ForeignKeys

· ForeignKeyColumns

· ProcedureParameters

标签: 暂无
最后更新:04/26/2011

guangjie

这个人很懒,什么都没留下

点赞
下一篇 >

文章评论

您需要 登录 之后才可以评论
最新 热点 随机
最新 热点 随机
Red Hat Enterprise Linux 9 发布,大量组件升级 windows10 家庭版添加hyper-v虚拟机组件 Red Hat Enterprise Linux 8.5 GA 发布 Rocky Linux 8.4 GA正式版发布下载 Rocky Linux 8.3 首个候选版本发布 CentOS 8.3 (2011) ISO镜像下载发布 CentOS Linux 落幕 CentOS Stream 上位 CentOS Linux 7.9 (2009) iso镜像下载 Windows 10 2020年10月更新ISO下载 ubuntu ssh通过密钥登陆服务器
imail服务器无法收发邮件,smtp正常,却没有监听25端口 更改linux默认启动方式 微软计划10月23日在上海举行Windows8发布会 Tencent QQ for Linux lvs keepalived mysqlcluster负载均衡DR 网易诚信邮箱计划 Windows Internet Explorer 8 简体中文正式版发布! 织梦淘宝客3.3整合版让taoke做主目录成首页 如何查看windows的版本号 linux下myql的安装
标签聚合
linux nginx Windows Server 2012 redhat springboot android Hyper-V php_curl
友情链接
  • linux运维
  • 郑州SEO

COPYRIGHT © 2021 gjie.cn. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备07002435号-8