C# Create Access File

  1           ///   <summary>
  2           ///  ADOX objects
  3           ///  创建Access数据
  4           ///  涂聚文 
  5           ///   </summary>
  6           private   void  createAccessTest()
  7          {
  8                   string  dbName  =  Server.MapPath( " ~ " +   " \\ "   +  Guid.NewGuid().ToString()  +   " .mdb " // "D:DataBaseFirstCatalog.mdb";
  9                  ADOX.CatalogClass catlog  =   new  ADOX.CatalogClass();
 10                  catlog.Create( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "   +  dbName  +   " ;Jet OLEDB:Database password=123456; "   +   " Jet OLEDB:Engine Type=5 " );
 11 
 12                  ADOX.TableClass table  =   new  ADOX.TableClass();
 13                  table.ParentCatalog  =  catlog;
 14                  table.Name  =   " FirstTable " ;
 15 
 16                   // StuId Column(AutoIncrement )
 17                  ADOX.ColumnClass col1  =   new  ADOX.ColumnClass();
 18                  col1.ParentCatalog  =  catlog;
 19                  col1.Type  =  ADOX.DataTypeEnum.adInteger; 
 20                  col1.Name  =   " StuId " ;
 21                  col1.Properties[ " Jet OLEDB:Allow Zero Length " ].Value  =   false ;
 22                  col1.Properties[ " AutoIncrement " ].Value  =   true ;
 23                 
 24                 
 25                   // Name Column
 26                  ADOX.ColumnClass col2  =   new  ADOX.ColumnClass();
 27                  col2.ParentCatalog  =  catlog;
 28                  col2.Name  =   " StuName " ;
 29                  col2.Properties[ " Jet OLEDB:Allow Zero Length " ].Value  =   false ;
 30                   
 31                   // Age Column
 32                  ADOX.ColumnClass col3  =   new  ADOX.ColumnClass();
 33                  col3.ParentCatalog  =  catlog;
 34                  col3.Name  =   " Stuage " ;
 35                  col3.Type  =  ADOX.DataTypeEnum.adDouble;
 36                  col3.Properties[ " Jet OLEDB:Allow Zero Length " ].Value  =   false ;
 37 
 38                   //  Primary
 39                  table.Keys.Append( " PrimaryKey " , ADOX.KeyTypeEnum.adKeyPrimary,  " StuId " "" "" );
 40                  table.Columns.Append(col1, ADOX.DataTypeEnum.adInteger,  0 );
 41                  table.Columns.Append(col3, ADOX.DataTypeEnum.adDouble,  666 );
 42                  table.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar,  25 );
 43                  catlog.Tables.Append(table);
 44 
 45                  System.Runtime.InteropServices.Marshal.ReleaseComObject(table);
 46                  System.Runtime.InteropServices.Marshal.ReleaseComObject(catlog);
 47                  table  =   null ;
 48                  catlog  =   null ;
 49                  GC.WaitForPendingFinalizers();
 50                  GC.Collect();
 51          }
 52           ///   <summary>
 53           ///  修改密码
 54           ///  using ADOX;
 55           ///  涂聚文 
 56           /// 引用COM:Microsoft ADO Ext. 2.8 for DDL and Security  
 57           /// 添加引用:Microsoft ActioveX Data Objects 2.8 Library
 58           ///  string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database Password=" + Pwd + ";Jet OLEDB:Engine Type=5"; 
 59           ///   </summary>
 60           ///   <param name="fileName"> 文件名称及地址 </param>
 61           ///   <param name="newpwd"> 新密码 </param>
 62           ///   <param name="openpwd"> 原密码 </param>
 63           private   void  AlterAccessPassword( string  fileName,  string  newpwd,  string  openpwd)
 64          {
 65               string  conn  =   " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "   +  fileName  +   " ;Jet OLEDB:Database password= "   +  openpwd;
 66               string  sql  =   " ALTER DATABASE PASSWORD  "   +  newpwd  +   "   "   +  openpwd;
 67              ADODB.Connection cn  =   new  ADODB.Connection();
 68              cn.Mode  =  ADODB.ConnectModeEnum.adModeShareExclusive;
 69              cn.Open(conn,  null null - 1 );
 70               //  执行 SQL 语句以更改密码。   
 71               object  num;
 72              cn.Execute(sql,  out  num,  - 1 );
 73              cn.Close();
 74          }
 75           ///   <summary>
 76           ///  Create MDB file
 77           ///  using System.IO;
 78           ///  涂聚文 
 79           ///   </summary>
 80           ///   <param name="filename"> 文件名称 </param>
 81           public   static   void  CreateAccessFile( string  filename)
 82          {
 83 
 84               if  ( ! Directory.Exists(AppDomain.CurrentDomain.BaseDirectory  +   " DataBase\\ " ))
 85              {
 86 
 87                  Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory  +   " DataBase\\ " );
 88 
 89              }
 90              ADOX.CatalogClass cat  =   new  CatalogClass();
 91               string  str  =   " provider=Microsoft.Jet.OleDb.4.0;Data Source= "   +  AppDomain.CurrentDomain.BaseDirectory  +   " DataBase\\ "   +  filename  +   " .mdb; " ;
 92 
 93              cat.Create(str);
 94 
 95              cat  =   null ;
 96 
 97          }
 98           ///   <summary>
 99           ///  创建MDB file
100           ///  using System.Runtime.InteropServices; 
101           ///  涂聚文
102           ///   </summary>
103           ///   <param name="myPath"> 文件地址 </param>
104           ///   <returns></returns>
105           public   bool  GenerateLogDatabase( string  myPath)
106          {   
107              CatalogClass cat  =   new  CatalogClass();
108               string  strSQL;
109               string  cs;
110 
111               try
112              {
113                  cs  =   " Provider=Microsoft.Jet.OLEDB.4.0; "   +
114                        " Data Source= "   +  myPath  +   " ; "   +
115                        " Jet OLEDB:Engine Type=5 " ;
116 
117                  strSQL  =   " CREATE TABLE Issues (mID AUTOINCREMENT, mUser TEXT(100) NOT NULL  "   +
118                            " , mError TEXT(100) NOT NULL,  "   +
119                            " mDescription TEXT(100) NOT NULL, mDate DATETIME NOT NULL) " ;
120 
121                  cat.Create(cs);
122 
123                   using  (OleDbConnection cnn  =   new  OleDbConnection(cs))
124                  {
125                      OleDbCommand cmd  =   new  OleDbCommand();
126                       try
127                      {
128                          cmd.CommandText  =  strSQL;
129                          cmd.CommandType  =  CommandType.Text;
130                          cmd.Connection  =  cnn;
131                          cnn.Open();
132                          cmd.ExecuteNonQuery();
133                          cnn.Close();
134                           return   true ;
135                      }
136                       catch  (Exception)
137                      {
138                           throw ;
139                      }
140                       finally
141                      {
142                          cnn.Close();
143                          cmd.Dispose();
144                      }
145                  }
146              }
147               catch  (Exception)
148              {
149                   throw ;
150              }
151               finally
152              {   
153                  Marshal.FinalReleaseComObject(cat);
154              }
155          }

http://msdn.microsoft.com/en-us/library/ms675541(v=vs.85).aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值