Web 在线数据建模

无需安装access用C#创建数据库,创建表

首先引用msadox.dll和msjro.dll(C:\Program Files\Common Files\System\ado\),msjro.dll可以从网上下载,然后引用

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


然后编写相关函数

  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.   
  13.          mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;  
  14.          // 创建一个CatalogClass对象的实例,     
  15.   
  16.          ADOX.CatalogClass cat = new ADOX.CatalogClass();  
  17.   
  18.          // 使用CatalogClass对象的Create方法创建ACCESS数据库     
  19.   
  20.          cat.Create(mdbPath);  
  21.   
  22.          //创建数据库后关闭连接  
  23.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);  
  24.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);  
  25.      }  
  26.      /// <summary>  
  27.      /// 连接数据库  
  28.      /// </summary>  
  29.      /// <param name="filename">文件名包含路径</param>  
  30.      /// <returns>返回连接字符串</returns>  
  31.      public ADODB.Connection Connection(string filename)  
  32.      {  
  33.          ADODB.Connection con = new ADODB.Connection();  
  34.          con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename, nullnull, -1);  
  35.           
  36.          return con;  
  37.            
  38.            
  39.      }  
  40.      /// <summary>  
  41.      /// 创建数据表  
  42.      /// </summary>  
  43.      /// <param name="con">连接字符串</param>  
  44.      /// <param name="table_name">表的名称</param>  
  45.      /// <param name="column">字段</param>  
  46.      public void CreateTable(ADODB.Connection con,string table_name,params Column[] column)  
  47.      {  
  48.          CatalogClass cat = new CatalogClass();  
  49.          cat.ActiveConnection = con;  
  50.          Table table = new Table();  
  51.   
  52.          Column col = new Column();  
  53.          col.ParentCatalog = cat;  
  54.          col.Type = DataTypeEnum.adInteger;  
  55.          col.Name = "ID";  
  56.          col.DefinedSize = 9;  
  57.          col.Properties["AutoIncrement"].Value = true;  
  58.   
  59.          table.Columns.Append(col, DataTypeEnum.adInteger, 9);  
  60.          table.Keys.Append("FirstPrimaryKey",KeyTypeEnum.adKeyPrimary,col, nullnull);  
  61.          table.Name = table_name;  
  62.          foreach (Column item in column)  
  63.          {  
  64.              table.Columns.Append(item);  
  65.          }  
  66.          cat.Tables.Append(table);  
  67.          //创建数据库后关闭连接  
  68.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);  
  69.          System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);  
  70.      }  



示例:

  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.         }  

 

posted @ 2013-08-16 09:17  NetUML大数据搜索  阅读(742)  评论(4编辑  收藏  举报
Web 在线数据建模