C#如何写DBHelper 类(数据库连接和操作)

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

namespace UseAdo
{
    public class DBHelper
    {

    //连接字符串
      public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["SQLConn"].ToString();

//非查询操作

        public static int ExecuteNonQuery(string commandText, CommandType commandType, params SqlParameter[] param)
        {
            int count = 0;
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;
                if (param != null)
                {
                    cmd.Parameters.AddRange(param);
                }
                con.Open();
                count = cmd.ExecuteNonQuery();
            }
            return count;
        }

//返回一个值得ExecuteScalar方法

        public static object ExecuteScalar(string commandText, CommandType commandType, params SqlParameter[] param)
        {
            object obj = null;
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;
                if (param != null)
                {
                    cmd.Parameters.AddRange(param);
                }
                con.Open();
                obj = cmd.ExecuteScalar();
            }
            return obj;
        }

 //询方法ExecuteReader()

        public static SqlDataReader ExecuteReader(string commandText, CommandType commandType, params SqlParameter[] param)
        {
            SqlDataReader reader = null;
            SqlConnection con = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = commandText;
            cmd.CommandType = commandType;
            if (param != null)
            {
                cmd.Parameters.AddRange(param);
            }
            try
            {
                con.Open();
                //关闭reader时同时关闭connection
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                con.Close();
            }
            return reader;
        }

//Adapter方法

        public static DataSet ExecuteDataSet(string commandText, CommandType commandType, params SqlParameter[] param)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;
                if (param != null)
                {
                    cmd.Parameters.AddRange(param);
                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);

                adapter.Fill(ds);
            }
            return ds;
        }
    }
}

你可能感兴趣的:(C#)