天天看點

無需安裝access用C#建立資料庫,建立表

首先引用msadox.dll和msjro.dll(C:\Program Files\Common Files\System\ado\),msjro.dll可以從網上下載下傳,然後引用

[csharp] view plain copy

  1. using ADOX;  
  2. using JRO;  
  3. using System.IO;  

然後編寫相關函數

[csharp] view plain copy

  1. /// <summary>  
  2.      /// 建立資料庫  
  3.      /// </summary>  
  4.      /// <param name="mdbPath">路徑</param>  
  5.      public void Create(string mdbPath)  
  6.      {  
  7.          if (File.Exists(mdbPath)) //檢查資料庫是否已存在     
  8.          {  
  9.              throw new Exception("目标資料庫已存在,無法建立");  
  10.          }  
  11.          // 可以加上密碼,這樣建立後的資料庫必須輸入密碼後才能打開     
  12.          mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;  
  13.          // 建立一個CatalogClass對象的執行個體,     
  14.          ADOX.CatalogClass cat = new ADOX.CatalogClass();  
  15.          // 使用CatalogClass對象的Create方法建立ACCESS資料庫     
  16.          cat.Create(mdbPath);  
  17.          //建立資料庫後關閉連接配接  
  18.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);  
  19.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);  
  20.      }  
  21.      /// <summary>  
  22.      /// 連接配接資料庫  
  23.      /// </summary>  
  24.      /// <param name="filename">檔案名包含路徑</param>  
  25.      /// <returns>傳回連接配接字元串</returns>  
  26.      public ADODB.Connection Connection(string filename)  
  27.      {  
  28.          ADODB.Connection con = new ADODB.Connection();  
  29.          con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename, null, null, -1);  
  30.          return con;  
  31.      }  
  32.      /// <summary>  
  33.      /// 建立資料表  
  34.      /// </summary>  
  35.      /// <param name="con">連接配接字元串</param>  
  36.      /// <param name="table_name">表的名稱</param>  
  37.      /// <param name="column">字段</param>  
  38.      public void CreateTable(ADODB.Connection con,string table_name,params Column[] column)  
  39.      {  
  40.          CatalogClass cat = new CatalogClass();  
  41.          cat.ActiveConnection = con;  
  42.          Table table = new Table();  
  43.          Column col = new Column();  
  44.          col.ParentCatalog = cat;  
  45.          col.Type = DataTypeEnum.adInteger;  
  46.          col.Name = "ID";  
  47.          col.DefinedSize = 9;  
  48.          col.Properties["AutoIncrement"].Value = true;  
  49.          table.Columns.Append(col, DataTypeEnum.adInteger, 9);  
  50.          table.Keys.Append("FirstPrimaryKey",KeyTypeEnum.adKeyPrimary,col, null, null);  
  51.          table.Name = table_name;  
  52.          foreach (Column item in column)  
  53.          {  
  54.              table.Columns.Append(item);  
  55.          }  
  56.          cat.Tables.Append(table);  
  57.          //建立資料庫後關閉連接配接  
  58.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);  
  59.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);  
  60.      }  

示例:

[csharp] view plain copy

  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             Create(Path.Combine(Application.StartupPath, "test.mdb"));  
  4.             Column col=new Column();  
  5.             col.Name="Name";  
  6.             col.Type=DataTypeEnum.adVarWChar;  
  7.             col.DefinedSize=50;  
  8.             CreateTable(Connection(Path.Combine(Application.StartupPath, "test.mdb")),"testtable", col);  
  9.         }  

轉載于:https://www.cnblogs.com/netuml/articles/3261306.html