C#连接Access数据库连接类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;


namespace Manage_Park
{
    class AccessHelper
    {
        private string conn_str = null;
        private OleDbConnection ole_connection = null;
        private OleDbCommand ole_command = null;
        private OleDbDataReader ole_reader = null;
        private DataTable dt = null;
        public AccessHelper()
        {
            conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\test\Manage_Park\Manage_Park\bin\Debug\stall.accdb";
            InitDB();
        }
        private void InitDB()
        {
            ole_connection = new OleDbConnection(conn_str);
            ole_command = new OleDbCommand();
        }
        public AccessHelper(string db_path)
        {
            conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + db_path + "'";
            InitDB();
        }
        private DataTable ConvertOleDbReaderToDataTable(ref OleDbDataReader reader)
        {
            DataTable dt_tmp = null;
            DataRow dr = null;
            int data_column_count = 0;
            int i = 0;


            data_column_count = reader.FieldCount;
            dt_tmp = BuildAndInitDataTable(data_column_count);


            if (dt_tmp == null)
            {
                return null;
            }


            while (reader.Read())
            {
                dr = dt_tmp.NewRow();


                for (i = 0; i < data_column_count; ++i)
                {
                    dr[i] = reader[i];
                }


                dt_tmp.Rows.Add(dr);
            }


            return dt_tmp;
        }
        private DataTable BuildAndInitDataTable(int Field_Count)
        {
            DataTable dt_tmp = null;
            DataColumn dc = null;
            int i = 0;


            if (Field_Count <= 0)
            {
                return null;
            }


            dt_tmp = new DataTable();


            for (i = 0; i < Field_Count; ++i)
            {
                dc = new DataColumn(i.ToString());
                dt_tmp.Columns.Add(dc);
            }


            return dt_tmp;
        }
        public DataTable GetDataTableFromDB(string strSql)
        {
            if (conn_str == null)
            {
                return null;
            }


            try
            {
                ole_connection.Open(); //打开数据库


                if (ole_connection.State == ConnectionState.Closed)
                {
                    return null;
                }


                ole_command.CommandText = strSql;
                ole_command.Connection = ole_connection;


                ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);


                dt = ConvertOleDbReaderToDataTable(ref ole_reader);


                ole_reader.Close();
                ole_reader.Dispose();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                if (ole_connection.State != ConnectionState.Closed)
                {
                    ole_connection.Close();
                }
            }


            return dt;
        }
        public int ExcuteSql(string strSql)
        {
            int nResult = 0;


            try
            {
                ole_connection.Open(); //打开数据库连接
                if (ole_connection.State == ConnectionState.Closed)
                {
                    return nResult;
                }


                ole_command.Connection = ole_connection;
                ole_command.CommandText = strSql;


                nResult = ole_command.ExecuteNonQuery();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return nResult;
            }
            finally
            {
                if (ole_connection.State != ConnectionState.Closed)
                {
                    ole_connection.Close();
                }
            }


            return nResult;
        }
    }

}



可以直接拿来用了,通用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值