Home > Database > Mysql Tutorial > C#操作Access之创建mdb库浅析

C#操作Access之创建mdb库浅析

WBOY
Release: 2016-06-07 15:43:57
Original
1373 people have browsed it

C#操作Access之创建mdb库是如何实现的呢?首先我们来看看基本的知识:本文C#操作Access数据库的基础知识,并提供一个相关的实例。C#的ADO.NET还不能通过编程方式创建全新的ACCESS(MDB)数据库,所以还只能使用ADOX这个来自COM的链接库来操作。 C#操作Access主

C#操作Access之创建mdb库是如何实现的呢?首先我们来看看基本的知识:本文C#操作Access数据库的基础知识,并提供一个相关的实例。C#的ADO.NET还不能通过编程方式创建全新的ACCESS(MDB)数据库,所以还只能使用ADOX这个来自COM的链接库来操作。

C#操作Access主要知识点如下:

<ol>
<li><span><span>using</span><span> System.Data.OleDb;  </span></span></li>
<li><span> </span></li>
<li>
<span>using</span><span> System.Data; </span>
</li>
</ol>
Copy after login

C#操作Access连接字符串:

<ol>
<li><span>String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;  </span></li>
<li><span> </span></li>
<li><span>Data Source=product.mdb"; </span></li>
</ol>
Copy after login

C#操作Access建立连接:

<ol><li><span><span>OleDbConnection connection = </span><span>new</span><span> OleDbConnection(connectionString); </span></span></li></ol>
Copy after login

C#操作Access使用OleDbCommand类来执行Sql语句:

<ol>
<li><span><span>OleDbCommand cmd = </span><span>new</span><span> OleDbCommand(sql, connection);  </span></span></li>
<li><span> </span></li>
<li><span>connection.Open();  </span></li>
<li><span> </span></li>
<li><span>cmd.ExecuteNonQuery(); </span></li>
</ol>
Copy after login

C#操作Access之创建mdb库,例程如下:

需要注意的是:参数mdbPath是mdb的完整路径(不包含表的名称)。

C#操作Access之创建mdb库实例:D:\\test.mdb

<ol>
<li><span><span>//创建mdb  </span><span> </span></span></li>
<li>
<span>public</span><span> </span><span>static</span><span> </span><span>bool</span><span> CreateMDBDataBase(</span><span>string</span><span> mdbPath)   </span>
</li>
<li><span>{  </span></li>
<li><span> </span></li>
<li>
<span>    </span><span>try</span><span> </span>
</li>
<li><span> </span></li>
<li><span>    {  </span></li>
<li><span> </span></li>
<li>
<span>ADOX.CatalogClass cat = </span><span>new</span><span> ADOX.CatalogClass();  </span>
</li>
<li><span> </span></li>
<li><span>cat.Create(  </span></li>
<li>
<span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span><span>   </span>
</li>
<li>
<span>+ mdbPath + </span><span>";"</span><span>);  </span>
</li>
<li><span> </span></li>
<li>
<span>cat = </span><span>null</span><span>;  </span>
</li>
<li><span> </span></li>
<li>
<span>return</span><span> </span><span>true</span><span>;  </span>
</li>
<li><span> </span></li>
<li><span>    }  </span></li>
<li><span> //C#操作Access之创建mdb库</span></li>
<li>
<span>    </span><span>catch</span><span> {  </span>
</li>
<li><span> </span></li>
<li>
<span>return</span><span> </span><span>false</span><span>;  </span>
</li>
<li><span> </span></li>
<li><span>}  </span></li>
<li><span> </span></li>
<li><span>} </span></li>
</ol>
Copy after login

C#操作Access之创建mdb库的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access之创建mdb库有所帮助。

Related labels:
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