程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 連接ACCESS數據庫類

連接ACCESS數據庫類

編輯:.NET實例教程

using System;
using System.Data;
using System.Data.OleDb;

namespace Bussiness.Cls
{
    /// <summary>
    /// UtilHelper 的摘要說明
    /// </summary>
    public class UtilHelper
    {
        public UtilHelper()
        { }

        public static string ConnString = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Application.StartupPath + "\\AnalyseFile\\DB\\DownLoad.mdb" + ";Persist Security Info=False;Jet OLEDB:Database PassWord=;";

        private static string PassWord = "xyz_@#$%^9&8*4()!_+ooyq#$%";

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static OleDbDataReader ExecuteReader(string sql)
        {
            try
            {
                //創建數據庫連接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    //創建command對象並保存sql查詢語句
                    OleDbCommand command = new OleDbCommand(sql, conn);

$False$

                    //創建datareader 對象來連接到表單
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        return reader;
                    }
                }
            }
            //一些通常的異常處理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] oledbparams)
        {<br />            try
            {
                //創建數據庫連接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    //創建command對象並保存sql查詢語句
                    OleDbCommand command = new OleDbCommand(sql, conn);
                    // 增加參數
                    if (oledbparams != null)
                    {
                        foreach (OleDbParameter p in oledbparams)
                        {
                            if (p != null)
                            {
                                if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && (p.Value == null))
                             {
                                    p.Value = DBNull.Value;
                                }
                                command.Parameters.Add(p);
                            }
                        }
                    }
                    //創建datareader 對象來連接到表單
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        return reader;
                    }
                }
            }
            //一些通常的異常處理
            catch (OleDbException ex)
&nbsp;           {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="sql"></param>
        public static void ExecuteNonQuery(string sql)
        {
            try
            {
                //創建數據庫連接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    //創建command對象並保存sql查詢語句
                    OleDbCommand command = new OleDbCommand(sql, conn);
                    if (conn.State == ConnectionState.Closed)
;              conn.Open();
         &nbsp;          //創建datareader 對象來連接到表單
                    command.ExecuteNonQuery();
                    conn.Close();
                }
            }
            //一些通常的異常處理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="sql"></param>
        public static void ExecuteNonQuery(string sql, params OleDbParameter[] oledbparams)
        {
            try
            {
                //創建數據庫連接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
    &nbsp;           {

                    //創建command對象並保存sql查詢語句
                    //    OleDbCommand command = new OleDbCommand(sql, conn);
                    OleDbCommand command = conn.CreateCommand();
                    command.CommandText = sql;
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    command.CommandType = CommandType.Text;
                    // 增加參數
                    if (oledbparams != null)
                    {
                        for (int i = 0; i < oledbparams.Length; i++)
                        {
                            command.Parameters.Add(oledbparams[i]);

                        }


                    }

                    //創建datareader 對象來連接到表單
                    command.ExecuteNonQuery();
                    conn.Close();
                }
            }
       //一些通常的異常處理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string sql)
        {
            try
            {
         ;      //創建數據庫連接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    using (OleDbDataAdapter ida = new OleDbDataAdapter(sql, conn))
                    {
                        DataSet ds = new DataSet();
                        ida.Fill(ds);
                        return ds;
                    }
                }
            }
            //一些通常的異常處理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
          &nbsp; {
                throw ex;
            }
        }       
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved