C#版本的DBHelper的代码

1,725 阅读1分钟

如下内容段是关于C#版本的DBHelper的内容。

using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections.Generic;

namespace DbHelper { public class SqlDbHelper :IDisposable { protected SqlConnection conn; protected SqlCommand cmd; protected SqlDataReader reader; protected SqlDataAdapter adapter; protected string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; public static SqlDbHelper Instance = null;

    public string ConnectionString
    {
        get { return this.connectionString; }
        set { this.connectionString = value; }
    }

    static SqlDbHelper()
    { SqlDbHelper.Instance = new SqlDbHelper(); }

    public SqlConnection GetConnection()
    {
        if (conn != null)
            return this.conn;
        return this.conn = new SqlConnection(connectionString);
    }

    public SqlConnection GetConnection(string _connStr)
    {
        if (this.conn != null)
            this.conn.ConnectionString = _connStr;
        else
            this.conn = new SqlConnection(_connStr);
        return this.conn;
    }

    private SqlCommand GetCommand(string sqlStr)
    {
        if (this.conn == null)
            this.conn = GetConnection();
        if (this.cmd == null)
            this.cmd = this.GetCommand(sqlStr, CommandType.Text, null);
        else
        {
			this.cmd.CommandText = sqlStr;
            this.cmd.CommandType = CommandType.Text;
            this.cmd.Parameters.Clear();
        }
        return this.cmd;
    }

    public SqlCommand GetCommand(string sqlStr, CommandType type, SqlParameter[] paras)
    {
        if (conn == null)
            this.conn = this.GetConnection();
        if (cmd == null)
            this.cmd = conn.CreateCommand();
        this.cmd.CommandType = type;
        this.cmd.CommandText = sqlStr;
        this.cmd.Parameters.Clear();
        if (paras != null)
            this.cmd.Parameters.AddRange(paras);
        return this.cmd;
    }

    public int ExecuteNonQuery(string sqlStr)
    {
        int line = -1;
        try { line = this.ExecuteNonQuery(sqlStr,CommandType.Text,null); }
        catch (SqlException e) { throw e; }
        return line;
    }

    public int ExecuteNonQuery(string sqlStr, CommandType type, SqlParameter[] paras)
    {
        int line = -1;
        CheckArgs(sqlStr);
        if (this.cmd == null)
            GetCommand(sqlStr, type, paras);
        this.cmd.Parameters.Clear();
        this.cmd.CommandText = sqlStr;
        this.cmd.CommandType = type;
        if(paras != null)
            this.cmd.Parameters.AddRange(paras);
        try { OpenConn(); line = this.cmd.ExecuteNonQuery(); }
        catch (SqlException e) { throw e; }
        return line;
    }

    public DataTable GetDataTable(string sqlStr)
    {
        CheckArgs(sqlStr);
        if (this.conn == null)
            this.conn = GetConnection();
        this.adapter = new SqlDataAdapter(sqlStr, this.conn);
        DataTable table = new DataTable();
        try { adapter.Fill(table); }
        catch (SqlException e) { throw e; }
        finally { this.adapter.Dispose(); }
        return table;
    }

    public SqlDataReader GetSqlDataReader(string sqlStr)
    {
        CheckArgs(sqlStr);
        if (cmd == null)
            GetCommand(sqlStr);
        if(reader != null)
            reader.Dispose();
        try { OpenConn(); this.reader = this.cmd.ExecuteReader(); }
        catch (SqlException e) { throw e; }
        return this.reader;
    }

    public bool ExecuteSqls(List<string> sqlCommands)
    {
        if (sqlCommands == null)
            throw new ArgumentNullException();
        if (sqlCommands.Count == 0)
            throw new ArgumentOutOfRangeException();
        if(this.cmd == null)
            GetCommand(null);
        SqlTransaction tran = null;
        try {
            OpenConn();
            tran = this.conn.BeginTransaction();
            this.cmd.Transaction = tran;
            foreach (string sql in sqlCommands)
            {
                if (ExecuteNonQuery(sql) == 0) 
                { tran.Rollback(); return false; }
            }
        }
        catch { if (tran != null) tran.Rollback(); throw; }
        tran.Commit();
        return true;
    }

    public virtual void Dispose()
    {
        if (this.reader != null)
        { reader.Dispose(); this.reader = null; }
        if (this.cmd != null)
        { this.cmd.Dispose(); this.cmd = null; }
        if (this.conn != null)
        { this.conn.Dispose(); conn = null; }
    }

    protected void OpenConn()
    {
        try {
            if (this.conn.State != ConnectionState.Open)
                conn.Open();
        }
        catch (SqlException e) { throw e; }
    }

    public void CloseConn()
    {
        if (this.conn != null && this.conn.State == ConnectionState.Open)
            this.conn.Close();
    }

    protected virtual void CheckArgs(string sqlStr)
    {
        if (sqlStr == null)
            throw new ArgumentNullException();
        if (sqlStr.Length == 0)
            throw new ArgumentOutOfRangeException();
    }

}

}