zoukankan      html  css  js  c++  java
  • DbHelper操作类

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

    namespace server
    {
        class Database
        {
            /// <summary>
            /// 连接数据库字符串
            /// </summary>
            private string connectionString;

            /// <summary>
            /// 存储数据库连接(保护类,只有由它派生的类才能访问)
            /// </summary>
            protected OleDbConnection Connection;

            /// <summary>
            /// 构造函数:数据库的默认连接
            /// </summary>
            public Database()
            {
                string connStr;
                connStr = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString.ToString();
               // connStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString(); //从web.config配置中读取
                connectionString = connStr;
                //connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + connStr;
               // connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
                //
                Connection = new OleDbConnection(connectionString);
            }

            /// <summary>
            /// 构造函数:带有参数的数据库连接
            /// </summary>
            /// <param name="newConnectionString"></param>
            public Database(string newConnectionString)
            {
                //connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + newConnectionString;
                connectionString = newConnectionString;
                Connection = new OleDbConnection(connectionString);
            }

            /// <summary>
            /// 获得连接字符串
            /// </summary>
            public string ConnectionString
            {
                get
                {
                    return connectionString;
                }
            }


            /// <summary>
            /// 执行SQL语句没有返回结果,如:执行删除、更新、插入等操作
            /// </summary>
            /// <param name="strSQL"></param>
            /// <returns>操作成功标志</returns>
            public bool ExeSQL(string strSQL)
            {
                bool resultState = false;

                Connection.Open();
                OleDbTransaction myTrans = Connection.BeginTransaction();
                OleDbCommand command = new OleDbCommand(strSQL, Connection, myTrans);

                try
                {
                    int n=command.ExecuteNonQuery();
                    myTrans.Commit();
                    resultState = true;
                }
                catch
                {
                    myTrans.Rollback();
                    resultState = false;
                }
                finally
                {
                    Connection.Close();
                }
                return resultState;
            }

            /// <summary>
            /// 执行SQL语句返回结果到DataReader中
            /// </summary>
            /// <param name="strSQL"></param>
            /// <returns>dataReader</returns>
            private OleDbDataReader ReturnDataReader(string strSQL)
            {
                Connection.Open();
                OleDbCommand command = new OleDbCommand(strSQL, Connection);
                OleDbDataReader dataReader = command.ExecuteReader();
               
                Connection.Close();

                return dataReader;
            }

            /// <summary>
            /// 执行SQL语句返回结果到DataSet中
            /// </summary>
            /// <param name="strSQL"></param>
            /// <returns>DataSet</returns>
            public DataSet ReturnDataSet(string strSQL)
            {
                Connection.Open();
                DataSet dataSet = new DataSet();
                OleDbDataAdapter OleDbDA = new OleDbDataAdapter(strSQL, Connection);
                OleDbDA.Fill(dataSet, "objDataSet");
               
                Connection.Close();
                return dataSet;
            }

            /// <summary>
            /// 执行一查询语句,同时返回查询结果数目
            /// </summary>
            /// <param name="strSQL"></param>
            /// <returns>sqlResultCount</returns>
            public int ReturnSqlResultCount(string strSQL)
            {
                int sqlResultCount = 0;

                try
                {
                    Connection.Open();
                    OleDbCommand command = new OleDbCommand(strSQL, Connection);
                    OleDbDataReader dataReader = command.ExecuteReader();
                    DataSet dataSet = new DataSet();
                   
                    while (dataReader.Read())
                    {
                        sqlResultCount++;
                    }
                    dataReader.Close();
                }
                catch
                {
                    sqlResultCount = 0;
                }
                finally
                {
                    Connection.Close();
                }
                return sqlResultCount;
            }

        }
    }

  • 相关阅读:
    算法学习(二)——树状数组求逆序数
    ZOJ 2412 Farm Irrigation
    排列的生成算法(一)
    汉诺塔递归实现
    汇编(五)
    汇编(四)
    汇编(三)
    汇编(二)
    0103MySQL中的B-tree索引 USINGWHERE和USING INDEX同时出现
    0103如何通过索引长度确定是否使用全索引
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1537757.html
Copyright © 2011-2022 走看看