C#访问数据库类

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Collections;
namespace DBUtility
{
    public class DataAccess
    {
        private string _confirString = "ConnectionString";
        public DataAccess()
        {
        }
        /// <param name="configString">app.config 关键字</param>
        public DataAccess(string configString)
        {
            ConfigString = configString;
        }
        /// <summary>
        /// 属性,设置数据库连接字符串
        /// </summary>
        public string ConfigString
        {
            get
            {
                return _confirString;
            }
            set
            {
                _confirString = value;
            }
        }
//===========================================GetProviderName=============================
        #region 获得数据库的类型public string GetProviderName(string ConfigString)
        /// <summary>
        /// 返回数据提供者
        /// </summary>
        /// <returns>返回数据提供者</returns>
        public string GetProviderName(string ConfigString)
        {
            ConnectionStringSettingsCollection ConfigStringCollention = ConfigurationManager.ConnectionStrings;
            if (ConfigStringCollention == null || ConfigStringCollention.Count <= 0)
            {
                throw new Exception("app.config 中无连接字符串!");
            }
            ConnectionStringSettings StringSettings = null;
            if (ConfigString == string.Empty)
            {
                StringSettings = ConfigurationManager.ConnectionStrings["ConnectionString"];
            }
            else
            {
                StringSettings = ConfigurationManager.ConnectionStrings[ConfigString];
            }
            return StringSettings.ProviderName;
        }
        /// <summary>
        /// 返回数据提供者
        /// </summary>
        /// <returns></returns>
        public string GetProviderName()
        {
            return GetProviderName(ConfigString);
        }
        #endregion
        //===========================================获得连接字符串==============================
        #region 获得连接字符串
        /// <summary>
        /// 获得连接字符串
        /// </summary>
        /// <returns></returns>
        private string GetConnectionString(string ConfigString)
        {
            ConnectionStringSettingsCollection ConfigStringCollention = ConfigurationManager.ConnectionStrings;
            if (ConfigStringCollention == null || ConfigStringCollention.Count <= 0)
            {
                throw new Exception("app.config 中无连接字符串!");
            }
            ConnectionStringSettings StringSettings = null;
            if (ConfigString == string.Empty)
            {
                StringSettings = ConfigurationManager.ConnectionStrings["ConnectionString"];
            }
            else
            {
                StringSettings = ConfigurationManager.ConnectionStrings[ConfigString];
            }
            return StringSettings.ConnectionString;
        }
        private string GetConnectionString()
        {
            return GetConnectionString(ConfigString);
        }
        #endregion
//===========================================GetDbproviderFactory========================
        #region 返回数据工厂  public DbProviderFactory GetDbProviderFactory()
        /// <summary>
        /// 返回数据工厂
        /// </summary>
        /// <returns></returns>
        private DbProviderFactory GetDbProviderFactory()
        {
            DbProviderFactory f = null;
            string ProviderName = GetProviderName();
            switch (ProviderName)
            {
                case "System.Data.SqlClient":
                    f = GetDbProviderFactory("System.Data.SqlClient");
                    break;
                case "System.Data.OracleClient":
                    f = GetDbProviderFactory("System.Data.OracleClient");
                    break;
                case "System.Data.OleDb":
                    f = GetDbProviderFactory("System.Data.OleDb");
                    break;
                default:
                    f = GetDbProviderFactory("System.Data.SqlClient");
                    break;
            }
            return f;
        }
        /// <summary>
        /// 返回数据工厂
        /// </summary>
        /// <param name="providername"></param>
        /// <returns></returns>
        private DbProviderFactory GetDbProviderFactory(string providername)
        {
            return DbProviderFactories.GetFactory(providername);
        }
        #endregion
//===========================================CreateConnection============================
        #region 创建数据库连接 public DbConnection CreateConnection()
        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <returns></returns>
        private DbConnection CreateConnection()
        {
            DbConnection con = GetDbProviderFactory().CreateConnection();
            con.ConnectionString = GetConnectionString();
            return con;
        }
        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <param name="provdername"></param>
        /// <returns></returns>
        private DbConnection CreateConnection(string provdername)
        {
            DbConnection con = GetDbProviderFactory(provdername).CreateConnection();
            con.ConnectionString = GetConnectionString();
            return con;
        }
        #endregion
//===========================================CreateCommand===============================
        #region 创建执行命令对象 public override DbCommand CreateCommand(string sql, CommandType cmdType, DbParameter[] parameters)
        /// <summary>
        /// 创建执行命令对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private DbCommand CreateCommand(string sql, CommandType cmdType, DbParameter[] parameters)
        {
            DbCommand _command = GetDbProviderFactory().CreateCommand();
            _command.Connection = CreateConnection();
            _command.CommandText = sql;
            _command.CommandType = cmdType;
            if (parameters != null && parameters.Length > 0)
            {
                foreach (DbParameter param in parameters)
                {
                    _command.Parameters.Add(param);
                }
            }
            return _command;
        }
        /// <summary>
        /// 创建执行命令对象
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>执行命令对象实例</returns>
        private DbCommand CreateCommand(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            return CreateCommand(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 创建执行命令对象
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>执行命令对象实例</returns>
        private DbCommand CreateCommand(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            return CreateCommand(sql, cmdtype, parameters);
        }
        /// <summary>
        /// 创建执行命令对象
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>执行命令对象实例</returns>
        private DbCommand CreateCommand(string sql, DbParameter[] parameters)
        {
            return CreateCommand(sql, CommandType.Text, parameters);
        }
        #endregion
        //===========================================CreateAdapter()=============================
        #region 创建数据适配器 CreateAdapter(string sql)
        /// <summary>
        /// 创建数据适配器
        /// </summary>
        /// <param name="sql">SQL,语句</param>
        /// <returns>数据适配器实例</returns>
        private DbDataAdapter CreateAdapter(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            return CreateAdapter(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 创建数据适配器
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <returns>数据适配器实例</returns>
        private DbDataAdapter CreateAdapter(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            return CreateAdapter(sql, cmdtype, parameters);
        }
        /// <summary>
        /// 创建数据适配器
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>数据适配器实例</returns>
        private DbDataAdapter CreateAdapter(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            DbConnection _connection = CreateConnection();
            DbCommand _command = GetDbProviderFactory().CreateCommand();
            _command.Connection = _connection;
            _command.CommandText = sql;
            _command.CommandType = cmdtype;
            if (parameters != null && parameters.Length > 0)
            {
                foreach (DbParameter _param in parameters)
                {
                    _command.Parameters.Add(_param);
                }
            }
            DbDataAdapter da = GetDbProviderFactory().CreateDataAdapter();
            da.SelectCommand = _command;
            return da;
        }
        #endregion
//===========================================CreateParameter=============================
        #region 生成参数 public override SqlParameter CreateParameter(string field, string dbtype, string value)
        /// <summary>
        /// 创建参数
        /// </summary>
        /// <param name="field">参数字段</param>
        /// <param name="dbtype">参数类型</param>
        /// <param name="value">参数值</param>
        /// <returns></returns>
        private DbParameter CreateParameter(string field, string dbtype, string value)
        {
            DbParameter p = GetDbProviderFactory().CreateParameter();
            p.ParameterName = field;
            p.Value = value;
            return p;
        }
        #endregion
        //===========================================ExecuteCommand()============================
        #region 执行非查询语句,并返回受影响的记录行数 ExecuteCommand(string sql)
        /// <summary>
        /// 执行非查询语句,并返回受影响的记录行数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>受影响记录行数</returns>
        public int ExecuteCommand(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            return ExecuteCommand(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行非查询语句,并返回受影响的记录行数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <returns>受影响记录行数</returns>
        public int ExecuteCommand(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            return ExecuteCommand(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行非查询语句,并返回受影响的记录行数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>受影响记录行数</returns>
        public int ExecuteCommand(string sql, DbParameter[] parameters)
        {
            return ExecuteCommand(sql, CommandType.Text, parameters);
        }
        /// <summary>
        ///批量执行SQL语句
        /// </summary>
        /// <param name="SqlList">SQL列表</param>
        /// <returns></returns>
        public bool ExecuteCommand(ArrayList SqlList)
        {
            DbConnection con = CreateConnection();
            con.Open();
            bool iserror = false;
            string strerror = "";
            DbTransaction SqlTran = con.BeginTransaction();
            try
            {
                for (int i = 0; i < SqlList.Count; i++)
                {
                    DbCommand _command = GetDbProviderFactory().CreateCommand();
                    _command.Connection = con;
                    _command.CommandText = SqlList[i].ToString();
                    _command.Transaction = SqlTran;
                    _command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                iserror = true;
                strerror = ex.Message;
            }
            finally
            {
                if (iserror)
                {
                    SqlTran.Rollback();
                    throw new Exception(strerror);
                }
                else
                {
                    SqlTran.Commit();
                }
                con.Close();
            }
            if (iserror)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 执行非查询语句,并返回受影响的记录行数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>受影响记录行数</returns>
        public int ExecuteCommand(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            int _result = 0;
            DbCommand _command = CreateCommand(sql, cmdtype, parameters);
            try
            {
                _command.Connection.Open();
                _result = _command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                _command.Connection.Close();
            }
            return _result;
        }
        #endregion
        //===========================================ExecuteScalar()=============================
        #region 执行非查询语句,并返回首行首列的值 ExecuteScalar(string sql)
        /// <summary>
        /// 执行非查询语句,并返回首行首列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>Object</returns>
        public object ExecuteScalar(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            return ExecuteScalar(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行非查询语句,并返回首行首列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <returns>Object</returns>
        public object ExecuteScalar(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            return ExecuteScalar(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行非查询语句,并返回首行首列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>Object</returns>
        public object ExecuteScalar(string sql, DbParameter[] parameters)
        {
            return ExecuteScalar(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行非查询语句,并返回首行首列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>Object</returns>
        public object ExecuteScalar(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            object _result = null;
            DbCommand _command = CreateCommand(sql, cmdtype, parameters);
            try
            {
                _command.Connection.Open();
                _result = _command.ExecuteScalar();
            }
            catch
            {
                throw;
            }
            finally
            {
                _command.Connection.Close();
            }
            return _result;
        }
        #endregion
        //===========================================ExecuteReader()=============================
        #region 执行查询,并以DataReader返回结果集  ExecuteReader(string sql)
        /// <summary>
        /// 执行查询,并以DataReader返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>IDataReader</returns>
        public DbDataReader ExecuteReader(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            return ExecuteReader(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行查询,并以DataReader返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <returns>IDataReader</returns>
        public DbDataReader ExecuteReader(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            return ExecuteReader(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行查询,并以DataReader返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>IDataReader</returns>
        public DbDataReader ExecuteReader(string sql, DbParameter[] parameters)
        {
            return ExecuteReader(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行查询,并以DataReader返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>IDataReader</returns>
        public DbDataReader ExecuteReader(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            DbDataReader _result;
            DbCommand _command = CreateCommand(sql, cmdtype, parameters);
            try
            {
                _command.Connection.Open();
                _result = _command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                throw;
            }
            finally
            {
            }
            return _result;
        }
        #endregion
        //===========================================GetDataSet()================================
        #region 执行查询,并以DataSet返回结果集 GetDataSet(string sql)
        /// <summary>
        /// 执行查询,并以DataSet返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            return GetDataSet(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行查询,并以DataSet返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <returns>DataSet</returns>
        public virtual DataSet GetDataSet(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            return GetDataSet(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行查询,并以DataSet返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataSet</returns>
        public virtual DataSet GetDataSet(string sql, DbParameter[] parameters)
        {
            return GetDataSet(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 执行查询,并以DataSet返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataSet</returns>
        public virtual DataSet GetDataSet(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            DataSet _result = new DataSet();
            IDataAdapter _dataAdapter = CreateAdapter(sql, cmdtype, parameters);
            try
            {
                _dataAdapter.Fill(_result);
            }
            catch
            {
                throw;
            }
            finally
            {
            }
            return _result;
        }
        /// <summary>
        /// 执行查询,并以DataSet返回指定记录的结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="StartIndex">开始索引</param>
        /// <param name="RecordCount">显示记录</param>
        /// <returns>DataSet</returns>
        public virtual DataSet GetDataSet(string sql, int StartIndex, int RecordCount)
        {
            return GetDataSet(sql, StartIndex, RecordCount);
        }
        #endregion
        //===========================================GetDataView()===============================
        #region 执行查询,并以DataView返回结果集   GetDataView(string sql)
        /// <summary>
        /// 执行查询,并以DataView返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataView</returns>
        public DataView GetDataView(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            DataView dv = GetDataSet(sql, CommandType.Text, parameters).Tables[0].DefaultView;
            return dv;
        }
        /// <summary>
        /// 执行查询,并以DataView返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataView</returns>
        public DataView GetDataView(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            DataView dv = GetDataSet(sql, cmdtype, parameters).Tables[0].DefaultView;
            return dv;
        }
        /// <summary>
        /// 执行查询,并以DataView返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataView</returns>
        public DataView GetDataView(string sql, DbParameter[] parameters)
        {
            DataView dv = GetDataSet(sql, CommandType.Text, parameters).Tables[0].DefaultView;
            return dv;
        }
        /// <summary>
        /// 执行查询,并以DataView返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataView</returns>
        public DataView GetDataView(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            DataView dv = GetDataSet(sql, cmdtype, parameters).Tables[0].DefaultView;
            return dv;
        }
        /// <summary>
        /// 执行查询,并以DataView返回指定记录的结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="StartIndex">开始索引</param>
        /// <param name="RecordCount">显示记录</param>
        /// <returns>DataView</returns>
        public DataView GetDataView(string sql, int StartIndex, int RecordCount)
        {
            return GetDataSet(sql, StartIndex, RecordCount).Tables[0].DefaultView;
        }
        #endregion
        //===========================================GetDataTable()==============================
        #region 执行查询,并以DataTable返回结果集   GetDataTable(string sql)
        /// <summary>
        /// 执行查询,并以DataTable返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql)
        {
            DbParameter[] parameters = new DbParameter[0];
            DataTable dt = GetDataSet(sql, CommandType.Text, parameters).Tables[0];
            return dt;
        }
        /// <summary>
        /// 执行查询,并以DataTable返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql, CommandType cmdtype)
        {
            DbParameter[] parameters = new DbParameter[0];
            DataTable dt = GetDataSet(sql, cmdtype, parameters).Tables[0];
            return dt;
        }
        /// <summary>
        /// 执行查询,并以DataTable返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql, DbParameter[] parameters)
        {
            DataTable dt = GetDataSet(sql, CommandType.Text, parameters).Tables[0];
            return dt;
        }
        /// <summary>
        /// 执行查询,并以DataTable返回结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdtype">命令类型</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql, CommandType cmdtype, DbParameter[] parameters)
        {
            DataTable dt = GetDataSet(sql, cmdtype, parameters).Tables[0];
            return dt;
        }
        /// <summary>
        /// 执行查询,并以DataTable返回指定记录的结果集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="StartIndex">开始索引</param>
        /// <param name="RecordCount">显示记录</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql, int StartIndex, int RecordCount)
        {
            return GetDataSet(sql, StartIndex, RecordCount).Tables[0];
        }
        /// <summary>
        /// 执行查询,返回以空行填充的指定条数记录集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="SizeCount">显示记录条数</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql, int SizeCount)
        {
            DataTable dt = GetDataSet(sql).Tables[0];
            int b = SizeCount - dt.Rows.Count;
            if (dt.Rows.Count < SizeCount)
            {
                for (int i = 0; i < b; i++)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        #endregion
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索sql
, string
, sql语句
, 返回datatable
, sql查询
, 语句
, sql 查询语句
, public
, sql数据库查询
, parameter
, commandtype
, sql 查询
, parameters
, 查询sql数据库
查询sql
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。

时间: 2024-12-17 19:24:23

C#访问数据库类的相关文章

ACCESS数据库访问的类

大部分ASP应用,都离不开对数据库的访问及操作,所以,对于数据库部分的访问操作,我们应该单独抽象出来,封装成一个单独的类.如果所用语言支持继承,可以封装一个这样的类,然后在数据操作层继承即可.下面是我写的一个ACCESS数据库访问的类,针对ACCESS作了优化,不过因为缺少足够的应用测试,可能仍然存在未知的bug及应用限制,主要代码如下: <% Class Oledb Private IDataPath Private IConnectionString Private Conn Private

MFC ADOC访问数据库用公共类报空指针异常,而不用公共类正常

问题描述 MFC ADOC访问数据库用公共类报空指针异常,而不用公共类正常 先建立了个公共类用于数据库连接查询如下ADOConn头文件#pragma once//导入AOC动态链接库#import ""C:Program FilesCommon FilesSystemadomsado15.dll"" no_namespace rename(""EOF""adoEOF"")rename(""

Access数据库访问助手类

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.Collections; namespace TaoBaoSyncLibrary.Dal { /// <summary> /// Access数据库访问助手类 /// </summary> public class DBHelper_Ac

通过WordPress数据库操作类wpdb访问数据库

为了方便访问mysql数据库,wordpress提供了一个全局变量$wpdb,通过$wpdb变量可以访问数据库中的任何数据表,可以实现对数据库的各项操作,包括建表.查询.删除.更新等,但是访问仅限于一个数据库,要访问多个数据库,需要重新实例化一个wpdb类. 使用的时候请不要直接调用$wpdb类中的方法.WordPress定义了$wpdb的全局变量,所以直接调用该全局变量$wpdb的实例来操作数据库.调用之前全局化变量$wpdb: global $wpdb; 查询: <?php $wpdb->

分享一下DbHelper通用数据库访问帮助类

通常我们在开发使用数据库访问帮助类时,都单独的针对某一种数据进行编写相应的数据访问帮助类,如SQLHelper,OracleHelper,OleDbHelper等,其实这些都帮助类的实现都是继承并实现ADO.NET中的数据库访问基类的,如SQLCommand->DbCommand,SQLConnection->DbConnection等,在未优化数据访问层之前,通常的框架结构如下所示: 其实我们现在可以将SQLServer,Oracle等的数据访问模块进行优化,将其合并为一个通用的DbHelp

Eclipse3.0配置SQLExplorer访问数据库

sql|访问|数据|数据库 英文原文地址: http://www.onjava.com/pub/a/onjava/2005/05/11/sqlexplorer.html中文地址: http://www.matrix.org.cn/resource/article/43/43630_Eclipse_SQLExplorer.html关键词: Eclipse SQLExplorer MySQL JDBC SQLExplorer是Eclipse集成开发环境的一种插件,它可以被用来从Eclipse连接到一

学习ADO和ODBC访问数据库的一些思路整理

ado|odbc|访问|数据|数据库 最近一直在学习研究用vc++访问数据库的问题,使用过ADO,也使用过ODBC.就这两种连接数据源的接口都有好几种方式,可以利用database,也可以利用dataset.利用这个word把我学习的思路整理一下. 一.使用ADO连接数据源 一般来说当建立基于对话框的应用程序时,都选择使用ADO比较方便(这纯属个人习惯问题). 1.直接在应用程序中建立与数据库的connection 2.自己写一个ADOConnection类作为应用程序操作数据库的接口,这样操作

SQLServer2000数据访问基类

server|server2000|sql|sqlserver|访问|数据 using System;using System.IO;using System.Data;using System.Data.SqlClient;using System.Configuration;using System.Collections; namespace SQLServerBase{    /// <summary>    ///内部类:存储过程的返回值记录类    /// </summary

用连接池提高Servlet访问数据库的效率 (-)

servlet|访问|数据|数据库 Java Servlet作为首选的服务器端数据处理技术,正在迅速取代CGI脚本.Servlet超越CGI的优势之一在于,不仅多个请求可以共享公用资源,而且还可以在不同用户请求之间保留持续数据.本文介绍一种充分发挥该特色的实用技术,即数据库连接池. 一.实现连接池的意义 动态Web站点往往用数据库存储的信息生成Web页面,每一个页面请求导致一次数据库访问.连接数据库不仅要开销一定的通讯和内存资源,还必须完成用户验证.安全上下文配置这类任务,因而往往成为最为耗时的