日期:2014-05-18  浏览次数:20783 次

C#操作数据库的方式那种好一点【DBHelper相关】
C# code



//////////////////////////////////////////////////第一种
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace  DAL
{
    public class DBHelper
    {
        string strCon =  Model.ConnectionModel.ConnectionString1;  
        private SqlConnection connection;
        public SqlConnection Connection
        {
            get
            {
                string connectionString = strCon;
                if (connection == null)
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }

       

        public int ExecuteCommand(string safeSql)
        {
            using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
            {
                int result = cmd.ExecuteNonQuery();
                return result;               
            }
        }

        public int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            using (SqlCommand cmd = new SqlCommand(sql, Connection))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
        }
       
        public int ExecuteCommand2(string sql, params SqlParameter[] values)
        {
            using (SqlCommand cmd = new SqlCommand(sql, Connection))
            {
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
        }

        public string GetScalar(string safeSql)
        {
            using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
            {

                string result = cmd.ExecuteScalar().ToString();
                return result;
            }
        }

        public string GetScalar(string sql, params SqlParameter[] values)
        {
            using (SqlCommand cmd = new SqlCommand(sql, Connection))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                string result = cmd.ExecuteScalar().ToString();
                return result;
            }
        }

        public SqlDataReader GetReader(string safeSql)
        {
            using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
            {

                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }
        }

        public SqlDataReader GetReader(string sql, params SqlParameter[] values)
        {
            using (SqlCommand cmd = new SqlCommand(sql, Connection))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }

        }

        public DataTable GetDataSet(string safeSql)
        {
            using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }

        public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {