• Access 获取数据库表名和字段名


    View Code
     1 //获取Access数据库表的字段名
     2         public List<string> GetTableFieldNameList(string TableName)
     3         {
     4             string conStr = "provider=microsoft.jet.oledb.4.0;";
     5             conStr += @"data source=" + 数据库的路径(例如:"D:\\data\\test.mdb" 6             List<string> list = new List<string>();
     7             OleDbConnection con = new OleDbConnection(conStr);
     8             try
     9             {
    10                 con.Open();
    11                 using (OleDbCommand cmd = new OleDbCommand())
    12                 {
    13                     cmd.CommandText = "SELECT TOP 1 * FROM [" + TableName + "]";
    14                     cmd.Connection = con;
    15                     OleDbDataReader dr = cmd.ExecuteReader();
    16                     for (int i = 0; i < dr.FieldCount; i++)
    17                     {
    18                         list.Add(dr.GetName(i));
    19                     }
    20                 }
    21                 return list;
    22             }
    23             catch (Exception e)
    24             { throw e; }
    25             finally
    26             {
    27                 if (con.State == ConnectionState.Open)
    28                     con.Close();
    29                 con.Dispose();
    30             }
    31         }
    View Code
     1 //获取Access数据库表名集合
     2         public ArrayList GetTableNameList(string filePath)
     3         {
     4             string conStr = "provider=microsoft.jet.oledb.4.0;";
     5             conStr += @"data source=" + filePath;
     6             ArrayList tableList = new ArrayList();
     7             OleDbConnection con = new OleDbConnection(conStr);
     8             try
     9             {
    10                 con.Open();
    11                 //第一种方法
    12                 /*DataRow [] rows = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Select();
    13                 for (int i = 0; i < rows.Length; i++)
    14                 {
    15                     if (!rows[i]["TABLE_NAME"].ToString().Contains("MSys"))
    16                     {
    17                         tableList.Add(rows[i]["TABLE_NAME"].ToString());
    18                     }
    19                 }
    20                 return tableList;*/
    21                 //第二种方法
    22                 DataTable dt = con.GetSchema("Tables");
    23                 foreach (DataRow row in dt.Rows)
    24                 {
    25                     if (row[3].ToString() == "TABLE")
    26                         tableList.Add(row[2].ToString());
    27                 }
    28                 return tableList;
    29             }
    30             catch (Exception e)
    31             { 
    32                 throw e; 
    33             }
    34             finally 
    35             { 
    36                 if (con.State == ConnectionState.Open)
    37                     con.Close();
    38                 con.Dispose(); 
    39             }
    40         }

    添加Access数据库的的字段名

    alter table tableName add column 字段名 int

  • 相关阅读:
    Javascript校验密码复杂度的正则表达式
    Git合并指定文件到另一个分支
    WebForm SignalR 实时消息推送
    基于SignalR的消息推送与二维码描登录实现
    yarn 错误There appears to be trouble with your network connection. Retrying...
    使用SqlConnectionStringBuilder构造数据库连接字符串
    邮箱核心业务领域建模
    C# NuGet常用命令
    阿里P7架构师是如何解决跨域问题的!你有遇到吗?
    WinForm 之 自定义标题栏的窗体移动
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/2703563.html
Copyright © 2020-2023  润新知