C#操作Access实例是怎么实现的呢?让我们来看看具体的代码: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.We
C#操作Access实例是怎么实现的呢?让我们来看看具体的代码:
<ol> <li><span><span>using</span><span> System; </span></span></li> <li> <span>using</span><span> System.Data; </span> </li> <li> <span>using</span><span> System.Configuration; </span> </li> <li> <span>using</span><span> System.Web; </span> </li> <li> <span>using</span><span> System.Web.Security; </span> </li> <li> <span>using</span><span> System.Web.UI; </span> </li> <li> <span>using</span><span> System.Web.UI.WebControls; </span> </li> <li> <span>using</span><span> System.Web.UI.WebControls.WebParts; </span> </li> <li> <span>using</span><span> System.Web.UI.HtmlControls; </span> </li> <li> <span>using</span><span> System.Data.OleDb; </span> </li> <li><span> </span></li> <li> <span>/// <summary> </summary></span><span> </span> </li> <li> <span>/// DataAccess 的摘要说明 C#操作Access实例解析</span><span> </span> </li> <li> <span>/// </span><span> </span> </li> <li> <span>public</span><span> </span><span>class</span><span> DataAccess </span> </li> <li><span>{ </span></li> <li> <span> </span><span>protected</span><span> </span><span>static</span><span> OleDbConnection conn = </span><span>new</span><span> OleDbConnection(); </span> </li> <li> <span> </span><span>protected</span><span> </span><span>static</span><span> OleDbCommand comm = </span><span>new</span><span> OleDbCommand(); </span> </li> <li> <span>public</span><span> DataAccess() </span> </li> <li><span>{ </span></li> <li> <span> </span><span>//init C#操作Access实例解析</span><span> </span> </li> <li><span>} </span></li> <li> <span> </span><span>private</span><span> </span><span>static</span><span> </span><span>void</span><span> openConnection() </span> </li> <li><span> { </span></li> <li> <span>if</span><span> (conn.State == ConnectionState.Closed) </span> </li> <li><span>{ </span></li> <li><span>conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0; </span></li> <li> <span>Data Source=</span><span>"+ConfigurationManager.AppSettings["</span><span>myconn"]; </span> </li> <li> <span>//web.config文件里设定。 </span><span> </span> </li> <li><span>comm.Connection = conn; </span></li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>conn.Open(); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li> <span>{ </span><span>throw</span><span> </span><span>new</span><span> Exception(e.Message); } </span> </li> <li><span> </span></li> <li><span>} </span></li> <li><span> </span></li> <li> <span> }</span><span>//打开数据库 C#操作Access实例解析</span> </li> <li><span> </span></li> <li> <span> </span><span>private</span><span> </span><span>static</span><span> </span><span>void</span><span> closeConnection() </span> </li> <li><span> { </span></li> <li> <span>if</span><span> (conn.State == ConnectionState.Open) </span> </li> <li><span>{ </span></li> <li><span>conn.Close(); </span></li> <li><span>conn.Dispose(); </span></li> <li><span>comm.Dispose(); </span></li> <li><span>} </span></li> <li> <span> }</span><span>//关闭数据库 C#操作Access实例解析</span><span> </span> </li> <li><span> </span></li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> </span><span>void</span><span> excuteSql(</span><span>string</span><span> sqlstr) </span> </li> <li><span> { </span></li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>comm.ExecuteNonQuery(); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li><span>{ </span></li> <li> <span>throw</span><span> </span><span>new</span><span> Exception(e.Message); </span> </li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ closeConnection(); } </span></li> <li> <span> }</span><span>//执行sql语句 C#操作Access实例解析</span><span> </span> </li> <li><span> </span></li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> OleDbDataReader dataReader(</span><span>string</span><span> sqlstr) </span> </li> <li><span> { </span></li> <li> <span>OleDbDataReader dr = </span><span>null</span><span>; </span> </li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span> </span></li> <li><span>dr = comm.ExecuteReader(CommandBehavior.CloseConnection); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> </span> </li> <li><span>{ </span></li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>dr.Close(); </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> { } </span> </li> <li><span>} </span></li> <li> <span>return</span><span> dr; </span> </li> <li><span>} </span></li> <li> <span>//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。 </span><span> </span> </li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> </span><span>void</span><span> dataReader(</span><span>string</span><span> sqlstr, </span> </li> <li> <span> </span><span>ref</span><span> OleDbDataReader dr) </span> </li> <li><span> { </span></li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>dr=comm.ExecuteReader(CommandBehavior.CloseConnection); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> </span> </li> <li><span>{ </span></li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li> <span>if</span><span> (dr != </span><span>null</span><span> && !dr.IsClosed) </span> </li> <li><span> dr.Close(); </span></li> <li><span>} //C#操作Access实例解析</span></li> <li> <span>catch</span><span> </span> </li> <li><span>{ </span></li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li><span>} </span></li> <li><span> } </span></li> <li> <span>//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭 </span><span> </span> </li> <li><span> </span></li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> DataSet dataSet(</span><span>string</span><span> sqlstr) </span> </li> <li><span> { </span></li> <li> <span>DataSet ds = </span><span>new</span><span> DataSet(); </span> </li> <li> <span>OleDbDataAdapter da = </span><span>new</span><span> OleDbDataAdapter(); </span> </li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>da.SelectCommand = comm; </span></li> <li><span>da.Fill(ds); </span></li> <li><span> </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li><span>{ </span></li> <li> <span>throw</span><span> </span><span>new</span><span> Exception(e.Message); </span> </li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li> <span>return</span><span> ds; </span> </li> <li> <span> }</span><span>//返回指定sql语句的dataset C#操作Access实例解析</span><span> </span> </li> <li><span> </span></li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> </span><span>void</span><span> dataSet( </span> </li> <li> <span>string</span><span> sqlstr, </span><span>ref</span><span> DataSet ds) </span> </li> <li><span> { </span></li> <li> <span>OleDbDataAdapter da = </span><span>new</span><span> OleDbDataAdapter(); </span> </li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>da.SelectCommand = comm; </span></li> <li><span>da.Fill(ds); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li><span>{ </span></li> <li> <span>throw</span><span> </span><span>new</span><span> Exception(e.Message); </span> </li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li> <span> }</span><span>//返回指定sql语句的dataset C#操作Access实例解析</span> </li> <li><span> </span></li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> DataTable dataTable(</span><span>string</span><span> sqlstr) </span> </li> <li><span> { </span></li> <li> <span>DataTable dt = </span><span>new</span><span> DataTable(); </span> </li> <li> <span>OleDbDataAdapter da = </span><span>new</span><span> OleDbDataAdapter(); </span> </li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>da.SelectCommand = comm; </span></li> <li><span>da.Fill(dt); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li><span>{ </span></li> <li> <span>throw</span><span> </span><span>new</span><span> Exception(e.Message); </span> </li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li> <span>return</span><span> dt; </span> </li> <li> <span> }</span><span>//返回指定sql语句的datatable </span><span> </span> </li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> </span><span>void</span><span> dataTable( </span> </li> <li> <span>string</span><span> sqlstr, </span><span>ref</span><span> DataTable dt) </span> </li> <li><span> { </span></li> <li> <span>OleDbDataAdapter da = </span><span>new</span><span> OleDbDataAdapter(); </span> </li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>da.SelectCommand = comm; </span></li> <li><span>da.Fill(dt); </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li><span>{ </span></li> <li> <span>throw</span><span> </span><span>new</span><span> Exception(e.Message); </span> </li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li> <span> }</span><span>//返回指定sql语句的datatable C#操作Access实例解析</span><span> </span> </li> <li><span> </span></li> <li> <span> </span><span>public</span><span> </span><span>static</span><span> DataView dataView(</span><span>string</span><span> sqlstr) </span> </li> <li><span> { </span></li> <li> <span>OleDbDataAdapter da = </span><span>new</span><span> OleDbDataAdapter(); </span> </li> <li> <span>DataView dv = </span><span>new</span><span> DataView(); </span> </li> <li> <span>DataSet ds = </span><span>new</span><span> DataSet(); </span> </li> <li> <span>try</span><span> </span> </li> <li><span>{ </span></li> <li><span>openConnection(); </span></li> <li><span>comm.CommandType = CommandType.Text; </span></li> <li><span>comm.CommandText = sqlstr; </span></li> <li><span>da.SelectCommand = comm; </span></li> <li><span>da.Fill(ds); </span></li> <li><span>dv = ds.Tables[0].DefaultView; </span></li> <li><span>} </span></li> <li> <span>catch</span><span> (Exception e) </span> </li> <li><span>{ </span></li> <li> <span>throw</span><span> </span><span>new</span><span> Exception(e.Message); </span> </li> <li><span>} </span></li> <li> <span>finally</span><span> </span> </li> <li><span>{ </span></li> <li><span>closeConnection(); </span></li> <li><span>} </span></li> <li> <span>return</span><span> dv; </span> </li> <li><span> } </span></li> <li> <span>//返回指定sql语句的dataview C#操作Access实例解析</span><span> </span> </li> <li><span> </span></li> <li><span>} </span></li> </ol>
C#操作Access实例解析的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access有所帮助。