国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

OLE DB取得數(shù)據(jù)庫的架構(gòu)信息

2024-07-21 02:22:56
字體:
供稿:網(wǎng)友

最大的網(wǎng)站源碼資源下載站,



關(guān)于如何取得數(shù)據(jù)庫架構(gòu)信息,對于取得sql server和oracal的數(shù)據(jù)庫結(jié)構(gòu)可能比較簡單,方法也比較多。

這里整理了一個對于所有能用ado.net鏈接的數(shù)據(jù)庫(比如access,db4格式的dbf自由表等)都通用的方法





1、首先就是鏈接各種數(shù)據(jù)庫的鏈接字符串,都是用ado.net,命名空間是:using system.data.oledb;

用幾種數(shù)據(jù)庫舉個例子,需要其他的數(shù)據(jù)庫鏈接可以到這里查:http://www.connectionstrings.com/

但要是ole db, oledbconnection (.net) 的鏈接方式。



sql server數(shù)據(jù)庫連接字符串      string sconnstr = "provider=sqloledb;data source=aron1;initial catalog=pubs;user id=sa;password=asdasd;" ;

access數(shù)據(jù)庫連接字符串

    string sconnstr = "provider=microsoft.jet.oledb.4.0;data source=/ omepath//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數(shù)據(jù)庫連接字符串

    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、取得數(shù)據(jù)庫架構(gòu)信息,這里我以取得數(shù)據(jù)庫的表格列表為例,代碼如下   





/// <summary>  /// 取得一個數(shù)據(jù)庫的表格列表,對所有用oledb連接的數(shù)據(jù)庫都可使用  /// </summary>  /// <param name="sconnstr">數(shù)據(jù)庫連接字符串</param>  /// <returns>表格列表數(shù)組</returns>  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"});

這一句是關(guān)鍵,是使用 oledbconnection 對象的 getoledbschematable 方法展示架構(gòu)信息

getoledbschematable 返回填充了架構(gòu)信息的 datatable。

你可以讓這個datatable直接顯示出來,就是詳細(xì)的信息了。



如果你需要查數(shù)據(jù)庫架構(gòu)的其他信息時,

比如數(shù)據(jù)源、表和視圖得到的目錄以及所存在的約束等。表中的架構(gòu)信息包括主鍵、列和自動編號字段。

只需要改變getoledbschematable的參數(shù)設(shè)置,具體參數(shù)可查看

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp



 

3、調(diào)用gettablist( sconnstr )就能返回那個數(shù)據(jù)庫的中表格列表

sconnstr 連接的是那種數(shù)據(jù)庫,只要是ado.net支持的就能返回結(jié)果。

當(dāng)連接的是udl文件的時候,想通過udl文件再連接到其他數(shù)據(jù)庫時,選用的驅(qū)動一定也要是oledb。

 

 



參考:

oledbschemaguid 成員http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp

how to:使用 getoledbschematable 和 visual c# .net 檢索架構(gòu)信息

http://support.microsoft.com/default.aspx?scid=kb;zh-cn;309681#3

connectionstrings

http://www.connectionstrings.com/


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永登县| 托克逊县| 岑溪市| 武隆县| 泌阳县| 丰城市| 榕江县| 康平县| 鄂尔多斯市| 浦城县| 巢湖市| 安仁县| 宁乡县| 库尔勒市| 隆回县| 定远县| 兴隆县| 兴业县| 陈巴尔虎旗| 华容县| 安福县| 深泽县| 盐津县| 南岸区| 南通市| 高碑店市| 重庆市| 东宁县| 拉孜县| 凤山县| 新疆| 济南市| 三亚市| 榆林市| 綦江县| 祁东县| 小金县| 田阳县| 阳西县| 松阳县| 河间市|