• ole辅助类sqlhelperaccess


    namespace dal
    {
        public class olehelp
        {
            private OleDbConnection conn = null;
            private OleDbCommand cmd = null;
            private OleDbDataReader oddr = null;
            public olehelp()
            {
                conn = new OleDbConnection(ConfigurationManager.AppSettings["connstring"].ToString());
            }
            private OleDbConnection getconn()
            {

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                return conn;
            }
            /// <summary>
            /// 执行不带参数的增删改语句
            /// </summary>
            /// <param name="cmdtext">增删改语句</param>
            /// <param name="ct">命令行模式</param>
            /// <returns>数值</returns>
            public int execnonquery(string cmdtext, CommandType ct)
            {
                int res;
                try
                {
                    cmd = new OleDbCommand(cmdtext, getconn());
                    cmd.CommandType = ct;
                    res = cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {

                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
                return res;
            }
            /// <summary>
            /// 执行带参数的增删改语句
            /// </summary>
            /// <param name="cmdtext"></param>
            /// <param name="paras"></param>
            /// <param name="ct"></param>
            /// <returns></returns>
            public int execnonquery(string cmdtext, OleDbParameter[] paras, CommandType ct)
            {
                int res;
                using (cmd = new OleDbCommand(cmdtext, getconn()))
                {
                    cmd.Parameters.AddRange(paras);
                    cmd.CommandType = ct;
                    res = cmd.ExecuteNonQuery();
                }
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }

                return res;
            }
            /// <summary>
            /// 执行不带参数查询语句
            /// </summary>
            /// <param name="cmdtext">语句</param>
            /// <param name="ct">命令格式</param>
            /// <returns>数据表</returns>
            public DataTable execquery(string cmdtext, CommandType ct)
            {

                DataTable dt = new DataTable();
                cmd = new OleDbCommand(cmdtext, getconn());
                cmd.CommandType = ct;
                using (oddr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    dt.Load(oddr);
                }

                return dt;
            }
            /// <summary>
            /// 执行带参数的查询语句
            /// </summary>
            /// <param name="cmdtext">语句</param>
            /// <param name="paras">参数</param>
            /// <param name="ct">命令格式</param>
            /// <returns>数据表</returns>
            public DataTable execquery(string cmdtext, OleDbParameter[] paras, CommandType ct)
            {

                DataTable dt = new DataTable();
                cmd = new OleDbCommand(cmdtext, getconn());
                cmd.Parameters.AddRange(paras);
                cmd.CommandType = ct;
                using (oddr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    dt.Load(oddr);
                }
                return dt;
            }

        }
    }

    要附加上这些:

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

    天道酬勤,厚积薄发。 君子之行,静以修身,俭以养德。 非淡泊无以明志,非宁静无以致远。 如有恒,何须三更起,半夜眠;最怕莫,三天打鱼两天晒网,竹篮打水一场空。
  • 相关阅读:
    List.Foreach与C#的foreach的区别
    他们突然觉得我懂的还挺多,嘎嘎~
    Mysql跨表更新 多表update sql语句总结
    “你没有权限登录JIRA”的解决办法
    iis日志查看
    自已写的Json序列化方法,可以序列话对象的只读属性
    mysql --limit
    python切片
    数据库
    tarzan-linux命令
  • 原文地址:https://www.cnblogs.com/houweidong/p/2917750.html
Copyright © 2020-2023  润新知