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

ASP.NET
ASP.NET开发:简化应用程序的开发支持Web标准
asp.net XMLHttpRequest实现用户注册前的验证
asp.net 页面间传值方法小结
asp.net url重写浅谈
asp.net 验证码生成和刷新及验证
C#精髓 GridView72大绝技 学习gridview的朋友必看
实例说明asp.net中的简单角色权限控制
asp.net网站开发包wq.dll打包下载
js与ASP.NET 中文乱码问题
asp.net checkbox 动态绑定id GridView删除提示
asp.net TextBox回车触发事件 图片在img显示
asp.net 脏字典过滤问题 用正则表达式来过滤脏数据
asp.NET 脏字过滤算法
asp.NET 脏字过滤算法 修改版
asp.net sql 数据库处理函数命令
asp.net Javascript 的几种写法与提示
ASP.NET MVC学习笔记
asp.net 中国身份证号码验证代码 非正则
Asp.net中使用Sqlite数据库的方法
asp.net 中文字符串提交乱码的解决方法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 208 ::
收藏到网摘: 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/