Home  >  Article  >  Database  >  C#动态创建Access数据库,创建加密码Access数据库,更改Access密

C#动态创建Access数据库,创建加密码Access数据库,更改Access密

WBOY
WBOYOriginal
2016-06-07 15:38:441104browse

以前工作中需要全新的Access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新数据库的时候释放出来,都感觉不爽,还是动态生成心理舒服。 生成数据库要使用ADO,首先添加引用。 using System.IO; using System.Data.OleDb; // 连接Access数据

  以前工作中需要全新的Access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新数据库的时候释放出来,都感觉不爽,还是动态生成心理舒服。

  生成数据库要使用ADO,首先添加引用。

<p><span>using</span><span> System.IO;<br></span><span>using</span><span> System.Data.OleDb;        <br></span><span>//</span><span>连接Access数据库</span><span><br></span><span>using</span><span> ADOX;                              <br></span><span>//</span><span>引用COM:Microsoft ADO Ext. 2.8 for DDL and Security                                             <br></span><span>//</span><span>添加引用:Microsoft ActioveX Data Objects 2.8 Library</span></p>

C#动态创建Access数据库,创建加密码Access数据库,更改Access密  创建数据库:

  然后使用ADODB创建数据库,直接看代码:

<span>   1:</span> <span>string</span> conn = <span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span> + fileName;
<span>   2:</span>             
<span>   3:</span> <span>//创建数据库</span>
<span>   4:</span> ADOX.Catalog catalog = <span>new</span> Catalog();
<span>   5:</span> <span>try</span>
<span>   6:</span> {
<span>   7:</span>     catalog.Create(conn); 
<span>   8:</span> }
<span>   9:</span> <span>catch</span>
<span>  10:</span> {}
<span>  11:</span>  
<span>  12:</span>  
<span>  13:</span> <span>//连接数据库</span>
<span>  14:</span> ADODB.Connection cn = <span>new</span> ADODB.Connection();
<span>  15:</span> cn.Open(conn, <span>null</span>, <span>null</span>, -1);                       
<span>  16:</span> catalog.ActiveConnection = cn;
<span>  17:</span>  
<span>  18:</span> <span>//新建表</span>
<span>  19:</span> ADOX.Table table = <span>new</span> ADOX.Table();
<span>  20:</span> table.Name = <span>"AdPlayList"</span>;
<span>  21:</span>  
<span>  22:</span> ADOX.Column column = <span>new</span> ADOX.Column();
<span>  23:</span> column.ParentCatalog = catalog;
<span>  24:</span> column.Type = ADOX.DataTypeEnum.adInteger; <span>// 必须先设置字段类型</span>
<span>  25:</span> column.Name = <span>"ID"</span>;
<span>  26:</span> column.DefinedSize = 9;
<span>  27:</span> column.Properties[<span>"AutoIncrement"</span>].Value = <span>true</span>;
<span>  28:</span> table.Columns.Append(column, DataTypeEnum.adInteger, 0);
<span>  29:</span> <span>//设置主键</span>
<span>  30:</span> table.Keys.Append(<span>"PrimaryKey"</span>, ADOX.KeyTypeEnum.adKeyPrimary, <span>"ID"</span>, <span>""</span>, <span>""</span>);
<span>  31:</span>  
<span>  32:</span> table.Columns.Append(<span>"FileName"</span>, DataTypeEnum.adVarWChar, 50);
<span>  33:</span> table.Columns.Append(<span>"FileDate"</span>, DataTypeEnum.adDate, 0);
<span>  34:</span> table.Columns.Append(<span>"FileSize"</span>, DataTypeEnum.adInteger, 9);
<span>  35:</span> table.Columns.Append(<span>"OrderID"</span>, DataTypeEnum.adInteger, 9);
<span>  36:</span> table.Columns.Append(<span>"Sha1"</span>, DataTypeEnum.adVarWChar, 50);
<span>  37:</span>  
<span>  38:</span> <span>try</span>
<span>  39:</span> {
<span>  40:</span>     catalog.Tables.Append(table);
<span>  41:</span> }
<span>  42:</span> <span>catch</span> (Exception ex)
<span>  43:</span> {
<span>  44:</span>     MessageBox.Show(ex.Message);
<span>  45:</span> }
<span>  46:</span> <span>//此处一定要关闭连接,否则添加数据时候会出错</span>
<span>  47:</span>  
<span>  48:</span> table = <span>null</span>;
<span>  49:</span> catalog = <span>null</span>;
<span>  50:</span> Application.DoEvents();
<span>  51:</span> cn.Close();

  创建加密码数据库

  创建加密数据库的时候可把我难住了,因为在Access中加密码的时候是以独占方式打开,然后加密码。所以总是想创建完数据库以后再加密,试了试没有成功,最后转变一下思想,在生成的时候加密码怎么样,一试果然成功了。

<span>   1:</span> <span>//创建不带密码的连接语句</span>
<span>   2:</span> <span>string</span> conn = <span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span> + fileName;
<span>   3:</span> <span>//创建加密码的连接语句,pwd是密码</span>
<span>   4:</span> <span>string</span> conn = <span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span> + fileName + <span>";<br>     Jet OLEDB:Database Password="</span> + Pwd + <span>";Jet OLEDB:Engine Type=5"</span>;            

  更改数据库密码:

  SQL更改数据库密码的语句是:

<p><span>ALTER</span><span>DATABASE</span><span> PASSWORD </span><span>[</span><span>NewPassword</span><span>]</span><span>[</span><span>OldPassword</span><span>]</span></p>

 

 

C#动态创建Access数据库,创建加密码Access数据库,更改Access密

  用OleDbConnection打开数据库执行此SQL语句根本不行,所以我就用ADODB打开数据库执行,但是报以下错误:

  给Access添加密码的时候会要求以“是以独占方式打开”,所以要设置打开的模式,详细说明如下:

  ADO ConnectModeEnum含义 设置或返回以下某个 ConnectModeEnum 的值。

常量                                          说明
AdModeUnknown                      默认值。表明权限尚未设置或无法确定。
AdModeRead                            表明权限为只读。
AdModeWrite                            表明权限为只写。
AdModeReadWrite                    表明权限为读/写。
AdModeShareDenyRead          防止其他用户使用读权限打开连接。
AdModeShareDenyWrite          防止其他用户使用写权限打开连接。
AdModeShareExclusive            防止其他用户打开连接。
AdModeShareDenyNone          防止其他用户使用任何权限打开连接。

  更改数据库代码如下:

<span>   1:</span> <span>string</span> conn = <span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span> + fileName + <span>";<br>      Jet OLEDB:Database password="</span> + openpwd;
<span>   2:</span> <span>string</span> sql = <span>"ALTER DATABASE PASSWORD "</span> + newpwd + <span>" "</span> + openpwd;
<span>   3:</span> ADODB.Connection cn = <span>new</span> ADODB.Connection();
<span>   4:</span> cn.Mode = ADODB.ConnectModeEnum.adModeShareExclusive;
<span>   5:</span> cn.Open(conn, <span>null</span>, <span>null</span>, -1);
<span>   6:</span> <span>// 执行 SQL 语句以更改密码。</span>
<span>   7:</span> <span>object</span> num;
<span>   8:</span> cn.Execute(sql, <span>out</span> num, -1);
<span>   9:</span> cn.Close();

  如果你要忘了加的密码,可以使用使用密码查看工具“破解密码unaccess”,在代码里面文件夹里呢。

  代码下载

  http://cid-4b517bbe6c0b58a0.office.live.com/self.aspx/.Public/cnblogsCsharp/CreateAccess.rar

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn