C#操做Access數據庫的基礎知識淺析web
C#操做Access數據庫的基礎知識都有什麼呢?sql
C#操做Access數據庫的基礎知識1.數據庫
經過ADO.NET的OleDb相關類來操做Access編程
主要知識點以下:數組
- using System.Data.OleDb;
- using System.Data;
鏈接字符串:ide
- String connectionString =
- "Provider=Microsoft.Jet.OLEDB.4.0;
- Data Source=product.mdb";
創建鏈接:學習
- OleDbConnection connection =
- new OleDbConnection(connectionString);
使用OleDbCommand類來執行Sql語句:spa
- OleDbCommand cmd = new OleDbCommand(sql, connection);
- connection.Open();
- cmd.ExecuteNonQuery();
C#操做Access數據庫的基礎知識2.對象
取得Access自增標識字段在插入數據後的id值事務
- cmd.CommandText = @"select @@identity";
- int value = Int32.Parse(cmd.ExecuteScalar().ToString());
- return value;
C#操做Access數據庫的基礎知識3.
執行事務
須要用到OleDbTransaction,關鍵語句以下:
- OleDbConnection connection = new OleDbConnection(connectionString);
- OleDbCommand cmd = new OleDbCommand();
- OleDbTransaction transaction = null;
- cmd.Connection = connection;
- connection.Open();
- transaction = connection.BeginTransaction();
- cmd.Transaction = transaction;
- cmd.CommandText=sql1;
- cmd.ExecuteNonQuery();
- cmd.CommandText=sql2;
- cmd.ExecuteNonQuery();
- transaction.Commit();
C#操做Access數據庫的基礎知識4.
執行查詢,返回DataSet
- OleDbConnection connection =
- new OleDbConnection(connectionString);
- DataSet ds = new DataSet();
- connection.Open();
- OleDbDataAdapter da =
- new OleDbDataAdapter(sql, connection);
- da.Fill(ds,"ds");
C#操做Access數據庫的基礎知識5.
分頁查詢
分頁查詢使用OleDbDataReader來讀取數據,並將結果寫到一個DataSet中返回。
以上內容封裝爲三個可重用的類:AccessDBUtil,AccessPageUtil,Page
C#操做Access數據庫的基礎知識的基本狀況就向你介紹到這裏,但願對你學習C#操做Access數據庫的基礎知識有所幫助。
C#操做Access之建立mdb庫是如何實現的呢?首先咱們來看看基本的知識:本文C#操做Access數據庫的基礎知識,並提供一個相關的實例。C#的ADO.NET還不能經過編程方式建立全新的ACCESS(MDB)數據庫,因此還只能使用ADOX這個來自COM的連接庫來操做。
C#操做Access主要知識點以下:
- using System.Data.OleDb;
- using System.Data;
C#操做Access鏈接字符串:
- String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
- Data Source=product.mdb";
C#操做Access創建鏈接:
- OleDbConnection connection = new OleDbConnection(connectionString);
C#操做Access使用OleDbCommand類來執行Sql語句:
- OleDbCommand cmd = new OleDbCommand(sql, connection);
- connection.Open();
- cmd.ExecuteNonQuery();
C#操做Access之建立mdb庫,例程以下:
須要注意的是:參數mdbPath是mdb的完整路徑(不包含表的名稱)。
C#操做Access之建立mdb庫實例:D:\\test.mdb
- //建立mdb
- public static bool CreateMDBDataBase(string mdbPath)
- {
- try
- {
- ADOX.CatalogClass cat = new ADOX.CatalogClass();
- cat.Create(
- "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
- + mdbPath + ";");
- cat = null;
- return true;
- }
- //C#操做Access之建立mdb庫
- catch {
- return false;
- }
- }
C#操做Access之建立mdb庫的基本內容就向你介紹到這裏,但願對你瞭解和學習C#操做Access之建立mdb庫有所幫助。
C#操做Access之建立表是如何實現的呢?首先咱們來看看基本的知識:本文C#操做Access數據庫的基礎知識,並提供一個相關的實例。C#的ADO.NET還不能經過編程方式建立全新的ACCESS(MDB)數據庫,因此還只能使用ADOX這個來自COM的連接庫來操做。
C#操做Access主要知識點以下:
- using System.Data.OleDb;
- using System.Data;
C#操做Access鏈接字符串:
- String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
- Data Source=product.mdb";
C#操做Access創建鏈接:
- OleDbConnection connection = new OleDbConnection(connectionString);
C#操做Access使用OleDbCommand類來執行Sql語句:
- OleDbCommand cmd = new OleDbCommand(sql, connection);
- connection.Open();
- cmd.ExecuteNonQuery();
C#操做Access之建立表,例程以下:
一般一個mdb的能夠包含n個表。下面的程序主要是建立一個table。
- //新建mdb的表,C#操做Access之建立表
- //mdbHead是一個ArrayList,存儲的是table表中的具體列名。
- public static bool CreateMDBTable(
- string mdbPath,string tableName, ArrayList mdbHead)
- {
- try
- {
- ADOX.CatalogClass cat = new ADOX.CatalogClass();
- string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;
- Data Source=" + mdbPath;
- ADODB.Connection cn = new ADODB.Connection();
- cn.Open(sAccessConnection, null, null, -1);
- cat.ActiveConnection = cn;
- //新建一個表,C#操做Access之建立表
- ADOX.TableClass tbl = new ADOX.TableClass();
- tbl.ParentCatalog = cat;
- tbl.Name = tableName;
- int size = mdbHead.Count;
- for (int i = 0; i < size; i++)
- {
- //增長一個文本字段
- ADOX.ColumnClass col2 = new ADOX.ColumnClass();
- col2.ParentCatalog = cat;
- col2.Name = mdbHead[i].ToString();//列的名稱
- col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
- tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarWChar, 500);
- }
- cat.Tables.Append(tbl);
- //這句把表加入數據庫(很是重要) ,C#操做Access之建立表
- tbl = null;
- cat = null;
- cn.Close();
- return true;
- }
- catch { return false; }
- }
C#操做Access之建立表的基本內容就向你介紹到這裏,但願對你瞭解和學習C#操做Access之建立表有所幫助。
C#操做Access之讀取mdb是如何實現的呢?首先咱們來看看基本的知識:本文C#操做Access數據庫的基礎知識,並提供一個相關的實例。C#的ADO.NET還不能經過編程方式建立全新的ACCESS(MDB)數據庫,因此還只能使用ADOX這個來自COM的連接庫來操做。
C#操做Access主要知識點以下:
- using System.Data.OleDb;
- using System.Data;
C#操做Access鏈接字符串:
- String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
- Data Source=product.mdb";
C#操做Access創建鏈接:
- OleDbConnection connection = new OleDbConnection(connectionString);
C#操做Access使用OleDbCommand類來執行Sql語句:
- OleDbCommand cmd = new OleDbCommand(sql, connection);
- connection.Open();
- cmd.ExecuteNonQuery();
C#操做Access之讀取mdb實例以下:
本例返回的是一個DataTable,如需其餘格式能夠自行轉換。
- // 讀取mdb數據
- public static DataTable ReadAllData(string tableName,
- string mdbPath,ref bool success)
- {
- DataTable dt = new DataTable();
- try
- {
- DataRow dr;
- //1、創建鏈接 C#操做Access之讀取mdb
- string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
- + mdbPath + ";Jet OLEDB:Database Password=haoren";
- OleDbConnection odcConnection = new OleDbConnection(strConn);
- //2、打開鏈接 C#操做Access之讀取mdb
- odcConnection.Open();
- //創建SQL查詢
- OleDbCommand odCommand = odcConnection.CreateCommand();
- //3、輸入查詢語句 C#操做Access之讀取mdb
- odCommand.CommandText = "select * from " + tableName;
- //創建讀取
- OleDbDataReader odrReader = odCommand.ExecuteReader();
- //查詢並顯示數據
- int size = odrReader.FieldCount;
- for (int i = 0; i < size; i++)
- {
- DataColumn dc;
- dc = new DataColumn(odrReader.GetName(i));
- dt.Columns.Add(dc);
- }
- while (odrReader.Read())
- {
- dr = dt.NewRow();
- for (int i = 0; i < size; i++)
- {
- dr[odrReader.GetName(i)] =
- odrReader[odrReader.GetName(i)].ToString();
- }
- dt.Rows.Add(dr);
- }
- //關閉鏈接 C#操做Access之讀取mdb
- odrReader.Close();
- odcConnection.Close();
- success = true;
- return dt;
- }
- catch
- {
- success = false;
- return dt;
- }
- }
C#操做Access之讀取mdb的基本內容就向你介紹到這裏,但願對你瞭解和學習C#操做Access之讀取mdb有所幫助。
C#操做Access之按列讀取mdb是如何實現的呢?首先咱們來看看基本的知識:本文C#操做Access數據庫的基礎知識,並提供一個相關的實例。C#的ADO.NET還不能經過編程方式建立全新的ACCESS(MDB)數據庫,因此還只能使用ADOX這個來自COM的連接庫來操做。
C#操做Access主要知識點以下:
- using System.Data.OleDb;
- using System.Data;
C#操做Access鏈接字符串:
- String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
- Data Source=product.mdb";
C#操做Access創建鏈接:
- OleDbConnection connection = new OleDbConnection(connectionString);
C#操做Access使用OleDbCommand類來執行Sql語句:
- OleDbCommand cmd = new OleDbCommand(sql, connection);
- connection.Open();
- cmd.ExecuteNonQuery();
C#操做Access之按列讀取mdb內容實例以下:
columns數組存儲的是你要查詢的列名稱(必須確保mdb表中存在你要的列)
- // 讀取mdb數據
- public static DataTable ReadDataByColumns(string mdbPaht,
- string tableName, string[] columns, ref bool success)
- {
- DataTable dt = new DataTable();
- try
- {
- DataRow dr;
- //1、創建鏈接 C#操做Access之按列讀取mdb
- string strConn = @"Provider=Microsoft.
- Jet.OLEDB.4.0;Data Source="
- + mdbPath + ";Jet OLEDB:Database Password=haoren";
- OleDbConnection odcConnection =
- new OleDbConnection(strConn);
- //2、打開鏈接 C#操做Access之按列讀取mdb
- odcConnection.Open();
- //創建SQL查詢
- OleDbCommand odCommand = odcConnection.CreateCommand();
- //3、輸入查詢語句
- string strColumn = "";
- for (int i = 0; i < columns.Length; i++)
- {
- strColumn += columns[i].ToString() + ",";
- }
- strColumn = strColumn.TrimEnd(',');
- odCommand.CommandText = "select "+strColumn+
- " from " + tableName;
- //創建讀取 C#操做Access之按列讀取mdb
- OleDbDataReader odrReader =
- odCommand.ExecuteReader();
- //查詢並顯示數據 C#操做Access之按列讀取mdb
- int size = odrReader.FieldCount;
- for (int i = 0; i < size; i++)
- {
- DataColumn dc;
- dc = new DataColumn(odrReader.GetName(i));
- dt.Columns.Add(dc);
- }
- while (odrReader.Read())
- {
- dr = dt.NewRow();
- for (int i = 0; i < size; i++)
- {
- dr[odrReader.GetName(i)] = odrReader[
- odrReader.GetName(i)].ToString();
- }
- dt.Rows.Add(dr);
- }
- //關閉鏈接 C#操做Access之按列讀取mdb
- odrReader.Close();
- odcConnection.Close();
- success = true;
- return dt;
- }
- catch
- {
- success = false;
- return dt;
- }
- }
C#操做Access之按列讀取mdb的基本內容就向你介紹到這裏,但願對你瞭解和學習C#操做Access之按列讀取mdb有所幫助。
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實例解析的基本內容就向你介紹到這裏,但願對你瞭解和學習C#操做Access有所幫助。