您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)C# 中怎么操作access數(shù)據(jù)庫(kù),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
隨筆:
(1) 命名空間
using System.Data.OleDb;
(2) 連接字符串
private staticstring connStr = @"Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb";
注意:
a、DataSource = 數(shù)據(jù)庫(kù)存放的路徑(這里數(shù)據(jù)庫(kù)放到了D盤(pán)目錄下)
b、 2003版本的Access數(shù)據(jù)庫(kù)鏈接字符串:
privatestatic stringconnStr =@"Provider = Microsoft.Jet.OLEDB.4.0;DataSource = d:\login.mdb"; 2007版本的Access數(shù)據(jù)庫(kù)鏈接字符串:privatestaticstring connStr =@"Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb";
(3) 建立連接:
OleDbConnection tempconn =new OleDbConnection(connStr);
(4) 使用OleDbCommand類來(lái)執(zhí)行Sql語(yǔ)句:
OleDbCommand cmd = new OleDbCommand(sql, tempconn); tempconn.Open(); cmd.ExecuteNonQuery();
栗子(工具類):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Windows.Forms; using System.Data; namespace test { class AccessHelper { private static string connStr = @"Provider = Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb"; public static OleDbConnection GetConn() { OleDbConnection tempconn = new OleDbConnection(connStr); MessageBox.Show(tempconn.DataSource); tempconn.Open(); MessageBox.Show(tempconn.State.ToString()); return (tempconn); } /// <summary> /// 執(zhí)行增加、刪除、修改指令 /// </summary> /// <param name="sql">增加、刪除、修改的sql語(yǔ)句</param> /// <param name="param">sql語(yǔ)句的參數(shù)</param> /// <returns></returns> public static int ExecuteNonQuery(string sql, params OleDbParameter[] param) { using (OleDbConnection conn = new OleDbConnection(connStr)) { using (OleDbCommand cmd = new OleDbCommand(sql,conn)) { if (param != null) { cmd.Parameters.AddRange(param); } conn.Open(); return(cmd.ExecuteNonQuery()); } } } /// <summary> /// 執(zhí)行查詢指令,獲取返回的首行首列的值 /// </summary> /// <param name="sql">查詢sql語(yǔ)句</param> /// <param name="param">sql語(yǔ)句的參數(shù)</param> /// <returns></returns> public static object ExecuteScalar(string sql, params OleDbParameter[] param) { using (OleDbConnection conn = new OleDbConnection(connStr)) { using (OleDbCommand cmd = new OleDbCommand(sql, conn)) { if (param != null) { cmd.Parameters.AddRange(param); } conn.Open(); return (cmd.ExecuteScalar()); } } } /// <summary> /// 執(zhí)行查詢指令,獲取返回的datareader /// </summary> /// <param name="sql">查詢sql語(yǔ)句</param> /// <param name="param">sql語(yǔ)句的參數(shù)</param> /// <returns></returns> public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] param) { OleDbConnection conn = new OleDbConnection(connStr); OleDbCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; cmd.CommandType = CommandType.Text; if (param != null) { cmd.Parameters.AddRange(param); } conn.Open(); return (cmd.ExecuteReader(CommandBehavior.CloseConnection)); } /// <summary> /// 執(zhí)行查詢指令,獲取返回datatable /// </summary> /// <param name="sql">查詢sql語(yǔ)句</param> /// <param name="param">sql語(yǔ)句的參數(shù)</param> /// <returns></returns> public static DataTable ExecuteDatable(string sql, params OleDbParameter[] param) { using (OleDbConnection conn = new OleDbConnection(connStr)) { using (OleDbCommand cmd = new OleDbCommand(sql, conn)) { if (param != null) { cmd.Parameters.AddRange(param); } DataTable dt = new DataTable(); OleDbDataAdapter sda = new OleDbDataAdapter(cmd); sda.Fill(dt); return (dt); } } } } }
以上就是C# 中怎么操作access數(shù)據(jù)庫(kù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。