VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之Access2007数据库下载地址与AccessHelper

链接:https://pan.baidu.com/s/1pLzOlTv0nqSbhzujHZht1w 
提取码:1m9l 

AccessHelper:

复制代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace AccessIReaderDemo
{
    public class AccessHelper
    {
        string str = @"provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\邯钢工作计划\物理站\华龙冲击\CBDdata2018.mdb";
        private static string Str;

        public AccessHelper(string _connstr)
        {
            Str = _connstr;
        }
        public static DataSet ExceDataSet(string sql, OleDbParameter[] parm = null,
            CommandType type = CommandType.Text)
        {
            using (OleDbConnection conn = new OleDbConnection(Str))
            {
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    if (parm != null)
                        cmd.Parameters.AddRange(parm);
                    OleDbDataAdapter abs = new OleDbDataAdapter(cmd);
                    DataSet set = new DataSet();
                    conn.Open();
                    abs.Fill(set);
                    return set;
                }
            }
        }

        /// <summary>
        /// reader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parm"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static OleDbDataReader Reader(string sql, OleDbParameter[] parm = null,
            CommandType type = CommandType.Text)
        {
            OleDbConnection conn = new OleDbConnection(Str);
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            if (parm != null)
                cmd.Parameters.AddRange(parm);
            conn.Open();
            OleDbDataReader reader = null;
            try
            {
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            }
            catch (Exception e)
            {

                throw;
            }
            return reader;
        }

        /// <summary>
        /// 查询首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parm"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object ExceScale(string sql, OleDbParameter[] parm = null, CommandType type = CommandType.Text)
        {
            using (OleDbConnection conn = new OleDbConnection(Str))
            {
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    if (parm != null)
                        cmd.Parameters.AddRange(parm);
                    cmd.CommandType = type;
                    conn.Open();
                    return cmd.ExecuteScalar();

                }
            }
        }

        /// <summary>
        /// 几行受影响
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parm"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int ExceQuery(string sql, OleDbParameter[] parm = null, CommandType type = CommandType.Text)
        {
            using (OleDbConnection conn = new OleDbConnection(Str))
            {
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    if (parm != null)
                        cmd.Parameters.AddRange(parm);
                    cmd.CommandType = type;
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

    }
}
复制代码

//Microsoft.ACE.OLEDB.12.0是连接access2007之后的数据库使用的
//Microsoft.Jet.OLEDB.4.0是连接access2003等数据库使用的。


相关教程