Home  >  Article  >  Database  >  C#操作Access之读取mdb浅析

C#操作Access之读取mdb浅析

WBOY
WBOYOriginal
2016-06-07 15:44:161189browse

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>

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>

C#操作Access建立连接:

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

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>

C#操作Access之读取mdb实例如下:

本例返回的是一个DataTable,如需其他格式可以自行转换。

<ol>
<li><span><span>// 读取mdb数据  </span><span> </span></span></li>
<li>
<span>public</span><span> </span><span>static</span><span> DataTable ReadAllData(</span><span>string</span><span> tableName,   </span>
</li>
<li>
<span>string</span><span> mdbPath,</span><span>ref</span><span> </span><span>bool</span><span> success)   </span>
</li>
<li><span>{   </span></li>
<li>
<span>    DataTable dt = </span><span>new</span><span> DataTable();   </span>
</li>
<li>
<span>    </span><span>try</span><span>   </span>
</li>
<li><span>    {   </span></li>
<li><span>DataRow dr;   </span></li>
<li><span> </span></li>
<li>
<span>//1、建立连接 C#操作Access之读取mdb </span><span> </span>
</li>
<li><span> </span></li>
<li>
<span>string</span><span> strConn = @</span><span>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span><span>   </span>
</li>
<li>
<span>+ mdbPath + </span><span>";Jet OLEDB:Database  Password=haoren"</span><span>;   </span>
</li>
<li>
<span>OleDbConnection odcConnection = </span><span>new</span><span> OleDbConnection(strConn);   </span>
</li>
<li><span> </span></li>
<li>
<span>//2、打开连接 C#操作Access之读取mdb </span><span> </span>
</li>
<li><span>odcConnection.Open();   </span></li>
<li><span> </span></li>
<li>
<span>//建立SQL查询  </span><span> </span>
</li>
<li><span>OleDbCommand odCommand = odcConnection.CreateCommand();   </span></li>
<li><span> </span></li>
<li>
<span>//3、输入查询语句 C#操作Access之读取mdb </span><span> </span>
</li>
<li><span> </span></li>
<li>
<span>odCommand.CommandText = </span><span>"select * from "</span><span> + tableName;   </span>
</li>
<li><span> </span></li>
<li>
<span>//建立读取  </span><span> </span>
</li>
<li><span>OleDbDataReader odrReader = odCommand.ExecuteReader();   </span></li>
<li><span> </span></li>
<li>
<span>//查询并显示数据  </span><span> </span>
</li>
<li>
<span>int</span><span> size = odrReader.FieldCount;   </span>
</li>
<li>
<span>for</span><span> (</span><span>int</span><span> i = 0; i </span>
</li>
<li><span>{   </span></li>
<li><span>    DataColumn dc;   </span></li>
<li>
<span>    dc = </span><span>new</span><span> DataColumn(odrReader.GetName(i));   </span>
</li>
<li><span>    dt.Columns.Add(dc);   </span></li>
<li><span>}   </span></li>
<li>
<span>while</span><span> (odrReader.Read())   </span>
</li>
<li><span>{   </span></li>
<li><span>    dr = dt.NewRow();   </span></li>
<li>
<span>    </span><span>for</span><span> (</span><span>int</span><span> i = 0; i </span>
</li>
<li><span>    {   </span></li>
<li><span>dr[odrReader.GetName(i)] =   </span></li>
<li><span>odrReader[odrReader.GetName(i)].ToString();   </span></li>
<li><span>    }   </span></li>
<li><span>    dt.Rows.Add(dr);   </span></li>
<li><span>}   </span></li>
<li>
<span>//关闭连接 C#操作Access之读取mdb </span><span> </span>
</li>
<li><span>odrReader.Close();   </span></li>
<li><span>odcConnection.Close();   </span></li>
<li>
<span>success = </span><span>true</span><span>;   </span>
</li>
<li>
<span>return</span><span> dt;   </span>
</li>
<li><span>    }   </span></li>
<li>
<span>    </span><span>catch</span><span>   </span>
</li>
<li><span>    {   </span></li>
<li>
<span>success = </span><span>false</span><span>;   </span>
</li>
<li>
<span>return</span><span> dt;   </span>
</li>
<li><span>    }   </span></li>
<li><span>} </span></li>
</ol>

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

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