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

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

WBOY
Release: 2016-06-07 15:38:44
Original
1122 people have browsed it

以前工作中需要全新的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>
Copy after login

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

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

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

  创建加密码数据库

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

<span>   1:</span> <span>//创建不带密码的连接语句</span>
Copy after login
<span>   2:</span> <span>string</span> conn = <span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span> + fileName;
Copy after login
<span>   3:</span> <span>//创建加密码的连接语句,pwd是密码</span>
Copy after login
<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>;            
Copy after login

  更改数据库密码:

  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>
Copy after login

 

 

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;
Copy after login
<span>   2:</span> <span>string</span> sql = <span>"ALTER DATABASE PASSWORD "</span> + newpwd + <span>" "</span> + openpwd;
Copy after login
<span>   3:</span> ADODB.Connection cn = <span>new</span> ADODB.Connection();
Copy after login
<span>   4:</span> cn.Mode = ADODB.ConnectModeEnum.adModeShareExclusive;
Copy after login
<span>   5:</span> cn.Open(conn, <span>null</span>, <span>null</span>, -1);
Copy after login
<span>   6:</span> <span>// 执行 SQL 语句以更改密码。</span>
Copy after login
<span>   7:</span> <span>object</span> num;
Copy after login
<span>   8:</span> cn.Execute(sql, <span>out</span> num, -1);
Copy after login
<span>   9:</span> cn.Close();
Copy after login

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

  代码下载

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template