aardio 官方社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 9256|回复: 1

得到access数据库的所有表名和字段名【整理】

[复制链接]

2

主题

5

回帖

45

积分

新手入门

积分
45
发表于 2014-4-23 11:38:13 | 显示全部楼层 |阅读模式
本帖最后由 walker 于 2014-4-23 11:45 编辑

根据这篇帖子(http://bbs.aardio.com/forum.php?mod=viewthread&tid=11931)整理,方便查阅。
建议将这两个功能扩展到库中哦。。
上代码:

  1. import access;
  2. import com;

  3. //打印access数据库中的所有表名
  4. //已排除系统表
  5. //walker
  6. PrintAllTableName = function(pathfile){
  7.         var db = access(pathfile);
  8.         if(!db){
  9.                 return false;        
  10.         }
  11.        
  12.         var cat = com.CreateObject("ADOX.Catalog")  
  13.         cat.ActiveConnection = db.connection;
  14.         console.log(pathfile + '的表:');
  15.         for index,tbl in ..com.each(cat.Tables) {
  16.                 if( tbl.Type = "TABLE") {
  17.                         console.log(tbl.Name);
  18.                 }
  19.         }
  20.        
  21.         db.close();
  22.         return true;
  23. }

  24. //打印access数据库中的所有表名
  25. //未排除系统表
  26. //qqmmcc
  27. PrintAllTableName_2 = function(pathfile){
  28.         conn = com.CreateObject("ADODB.Connection")
  29.         conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathfile;
  30.         conn.open();
  31.         rs = conn.OpenSchema("20")
  32.        
  33.         rs.MoveFirst()
  34.         console.log(pathfile + '的表:');
  35.         while(rs.EOF == false){
  36.                 console.log(rs("TABLE_NAME").value)
  37.             rs.MoveNext()
  38.         }
  39. }

  40. //打印所有字段名
  41. //alajia
  42. PrintAllFields = function(pathfile, tblname){
  43.         var db = access(pathfile);
  44.         if(!db){
  45.                 return false;        
  46.         }
  47.        
  48.         var cat = com.CreateObject("ADOX.Catalog");  
  49.     cat.ActiveConnection = db.connection;
  50.     console.log(pathfile +'表' + tblname + '的字段:');
  51.     for index,tbl in com.each(cat.Tables) {
  52.         if( tbl.Type = "TABLE" && tbl.name == tblname){
  53.             for(i=0; tbl.Columns.Count-1 ; 1){ //注意:从0开始
  54.                 console.log(tbl.Columns(i).Name);  //列出表头
  55.             }
  56.         }  
  57.     }
  58. }

  59. import console;
  60. PrintAllTableName("D:\test.mdb");
  61. PrintAllTableName_2("D:\test.mdb");
  62. PrintAllFields("D:\test.mdb", "testTable");

  63. console.pause() //按任意键继续
  64. ;

复制代码

1

主题

9

回帖

131

积分

一级会员

积分
131
发表于 2014-11-21 15:38:12 | 显示全部楼层
搞了半天没搞定,才发现,就在此处
特别感谢楼主
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2025-3-17 10:13 , Processed in 0.063685 second(s), 24 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表