Home > Database > Mysql Tutorial > body text

C#操作Access之按列读取mdb浅析

WBOY
Release: 2016-06-07 15:44:11
Original
1286 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内容实例如下:

columns数组存储的是你要查询的列名称(必须确保mdb表中存在你要的列)

<ol>
<li><span><span>// 读取mdb数据  </span><span> </span></span></li>
<li>
<span>public</span><span> </span><span>static</span><span> DataTable ReadDataByColumns(</span><span>string</span><span> mdbPaht,</span>
</li>
<li>
<span>string</span><span> tableName, </span><span>string</span><span>[] columns, </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>//1、建立连接 C#操作Access之按列读取mdb </span><span> </span>
</li>
<li>
<span>string</span><span> strConn = @</span><span>"Provider=Microsoft.</span>
</li>
<li>
<span>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></li>
<li>
<span>new</span><span> OleDbConnection(strConn);   </span>
</li>
<li>
<span>//2、打开连接 C#操作Access之按列读取mdb </span><span> </span>
</li>
<li><span>odcConnection.Open();   </span></li>
<li>
<span>//建立SQL查询  </span><span> </span>
</li>
<li><span>OleDbCommand odCommand = odcConnection.CreateCommand();   </span></li>
<li>
<span>//3、输入查询语句  </span><span> </span>
</li>
<li>
<span>string</span><span> strColumn = </span><span>""</span><span>;   </span>
</li>
<li>
<span>for</span><span> (</span><span>int</span><span> i = 0; i </span>
</li>
<li><span>{   </span></li>
<li>
<span>    strColumn += columns[i].ToString() + </span><span>","</span><span>;   </span>
</li>
<li><span>}   </span></li>
<li>
<span>strColumn = strColumn.TrimEnd(</span><span>','</span><span>);   </span>
</li>
<li>
<span>odCommand.CommandText = </span><span>"select "</span><span>+strColumn+</span>
</li>
<li>
<span>" from "</span><span> + tableName;   </span>
</li>
<li>
<span>//建立读取 C#操作Access之按列读取mdb </span><span> </span>
</li>
<li><span>OleDbDataReader odrReader = </span></li>
<li><span>odCommand.ExecuteReader();   </span></li>
<li>
<span>//查询并显示数据 C#操作Access之按列读取mdb </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> </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)] = odrReader[</span></li>
<li><span>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>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!