您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關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.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; /// <summary> /// DataAccess 的摘要說明 C#操作Access實例解析 /// </summary> public class DataAccess { protected static OleDbConnection conn = new OleDbConnection(); protected static OleDbCommand comm = new OleDbCommand(); public DataAccess() { //init C#操作Access實例解析 } private static void openConnection() { if (conn.State == ConnectionState.Closed) { conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0; Data Source="+ConfigurationManager.AppSettings["myconn"]; //web.config文件里設定。 comm.Connection = conn; try { conn.Open(); } catch (Exception e) { throw new Exception(e.Message); } } }//打開數據庫 C#操作Access實例解析 private static void closeConnection() { if (conn.State == ConnectionState.Open) { conn.Close(); conn.Dispose(); comm.Dispose(); } }//關閉數據庫 C#操作Access實例解析 public static void excuteSql(string sqlstr) { try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; comm.ExecuteNonQuery(); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } }//執行sql語句 C#操作Access實例解析 public static OleDbDataReader dataReader(string sqlstr) { OleDbDataReader dr = null; try { openConnection(); comm.CommandText = sqlstr; comm.CommandType = CommandType.Text; dr = comm.ExecuteReader(CommandBehavior.CloseConnection); } catch { try { dr.Close(); closeConnection(); } catch { } } return dr; } //返回指定sql語句的OleDbDataReader對象,使用時請注意關閉這個對象。 public static void dataReader(string sqlstr, ref OleDbDataReader dr) { try { openConnection(); comm.CommandText = sqlstr; comm.CommandType = CommandType.Text; dr=comm.ExecuteReader(CommandBehavior.CloseConnection); } catch { try { if (dr != null && !dr.IsClosed) dr.Close(); } //C#操作Access實例解析catch { } finally { closeConnection(); } } } //返回指定sql語句的OleDbDataReader對象,使用時請注意關閉 public static DataSet dataSet(string sqlstr) { DataSet ds = new DataSet(); OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(ds); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return ds; }//返回指定sql語句的dataset C#操作Access實例解析 public static void dataSet( string sqlstr, ref DataSet ds) { OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(ds); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } }//返回指定sql語句的dataset C#操作Access實例解析 public static DataTable dataTable(string sqlstr) { DataTable dt = new DataTable(); OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(dt); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return dt; }//返回指定sql語句的datatable public static void dataTable( string sqlstr, ref DataTable dt) { OleDbDataAdapter da = new OleDbDataAdapter(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(dt); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } }//返回指定sql語句的datatable C#操作Access實例解析 public static DataView dataView(string sqlstr) { OleDbDataAdapter da = new OleDbDataAdapter(); DataView dv = new DataView(); DataSet ds = new DataSet(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(ds); dv = ds.Tables[0].DefaultView; } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return dv; } //返回指定sql語句的dataview C#操作Access實例解析 }
上述就是小編為大家分享的C#中怎么操作Access了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。