当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > OLE DB取得数据库的架构信息

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

ASP.NET 中的 OLE DB取得数据库的架构信息


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 122 ::
收藏到网摘: n/a

关于如何取得数据库架构信息,对于取得SQL Server和Oracal的数据库结构可能比较简单,方法也比较多。这里整理了一个对于所有能用ADO.Net链接的数据库(比如Access,db4格式的dbf自由表等)都通用的方法 1、首先就是链接各种数据库的链接字符串,都是用ADO.Net,命名空间是:using System.Data.OleDb;用几种数据库举个例子,需要其他的数据库链接可以到这里查:http://www.connectionstrings.com/但要是OLE DB, OleDbConnection (.NET) 的链接方式。Sql Server数据库连接字符串 string sConnStr = "Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;" ;Access数据库连接字符串 string sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\somepath\\mydb.mdb;User Id=admin;Password=;";Dbf表(DB4格式)连接字符串 string sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\folder;Extended Properties=dBASE IV;User ID=Admin;Password=" ;FoxPro数据库连接字符串 string sConnStr = "Provider=vfpoledb.1;Data Source=C:\\MyDataDirectory\\;Collating Sequence=general" ;Excel文件连接字符串 string sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";Udl文件连接字符串 string sConnStr = "File Name=c:\\myDataLink.udl;" ; 2、取得数据库架构信息,这里我以取得数据库的表格列表为例,代码如下 /// /// 取得一个数据库的表格列表,对所有用OleDb连接的数据库都可使用 /// ///

数据库连接字符串
/// 表格列表数组 public string[] GetTabList( string sConnStr ) { if( sConnStr == null ) throw new ArgumentNullException( "sConnStr" ); string[] sTBList; using( OleDbConnection conn = new OleDbConnection( sConnStr ) ) { conn.Open(); DataTable dt = conn.GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object[] {null, null, null, "TABLE"}); conn.Close(); if ( dt.Rows.Count > 0 ) { sTBList = new string[dt.Rows.Count]; int i = 0; foreach ( DataRow dr in dt.Rows ) { sTBList[i] = dr["TABLE_NAME"].ToString(); i += 1; } } else { sTBList = new string[1]; sTBList[0] = "<没有表格>"; } } return sTBList; } 其中DataTable dt = conn.GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object[] {null, null, null, "TABLE"});这一句是关键,是使用 OleDbConnection 对象的 GetOleDbSchemaTable 方法展示架构信息GetOleDbSchemaTable 返回填充了架构信息的 DataTable。你可以让这个DataTable直接显示出来,就是详细的信息了。如果你需要查数据库架构的其他信息时,比如数据源、表和视图得到的目录以及所存在的约束等。表中的架构信息包括主键、列和自动编号字段。只需要改变GetOleDbSchemaTable的参数设置,具体参数可查看http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp 3、调用GetTabList( sConnStr )就能返回那个数据库的中表格列表sConnStr 连接的是那种数据库,只要是ADO.Net支持的就能返回结果。当连接的是Udl文件的时候,想通过Udl文件再连接到其他数据库时,选用的驱动一定也要是OleDB。 参考:OleDbSchemaGuid 成员http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.aspHOW TO:使用 GetOleDbSchemaTable 和 Visual C# .NET 检索架构信息http://support.microsoft.com/default.aspx?scid=kb;zh-cn;309681#3ConnectionStringshttp://www.connectionstrings.com/