c#实现对SQLserver2008数据库增、删、改、查操作的封装(DataBase.cs类)

DataBase.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration; //连接配置
using System.Data.SqlClient;//数据库类
using System.Data;
using System.Data.OleDb;

namespace NETSystem
{
    class DataBase
    {
        private SqlConnection conn;

        //获取配置文件中的连接字符串
        private string myschool = ConfigurationManager.ConnectionStrings["myschool"].ToString();
        //构造函数
        public DataBase()
        {
            conn = new SqlConnection(myschool);
        }
        /// 
        /// 增删改公用方法
        /// 
        /// 
        /// 
        /// 
        /*
         * 调用方式:
           DataBase db=new DataBase();     
           SqlParameter[] pars = new SqlParameter[] {
                new SqlParameter("@a",password),
                new SqlParameter("@b",name),
                new SqlParameter("@c",id)
            };       
            db.AduExecute("update User set password=@a , name=@b where id = @c", pars);
         */
        public bool AduExecute(string sqlText, params  SqlParameter[] parameters)
        {
            bool flag = false;
            try
            {
                //打开数据库连接
                conn.Open();
                //建立连接,并执行sql语句命令
                SqlCommand command = new SqlCommand(sqlText,conn);
                //如果参数集合不为空则进行遍历
                if (parameters != null) 
                {
                    foreach (SqlParameter p in parameters)
                    {
                        command.Parameters.Add(p);
                    }
                }
 
                
                int num = command.ExecuteNonQuery();
                if (num > 0)
                {
                    flag = true;
                }
            }
            catch (Exception)
            {
            }
            finally 
            {
                //关闭数据库连接
                conn.Close();
            }
            return flag;
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        /*
         * 调用方式:
            DataBase db=new DataBase();  
            string sql = "select id,name,age from  student";
            SqlDataReader dr = db.Select(sql);
            while (dr.Read())
            {               
                int id=Convert.ToInt32(dr[0].ToString());
                string name = dr[1].ToString();
                int age=Convert.ToInt32(dr[2].ToString());
            }
         */
        public SqlDataReader Select(string sqlText)
        {
            SqlDataReader dr = null;
            try
            {
                //打开数据库连接
                conn.Open();
                //建立连接,并执行sql语句命令
                SqlCommand command = new SqlCommand(sqlText, conn);
                //CommandBehavior.CloseConnection会自动关闭连接
                dr=command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception)
            {

            }
            return dr;
        }
        /// 
        /// 获取记录数
        /// 
        /// 
        /// 
        /*
         * 调用方式:
            DataBase db=new DataBase();  
            string sql = "select count(*) from  student";
            int count = db.Count(sql);
            MessageBox.Show(string.Format("数据库里共有{0}条记录", count), "提示");
         */
        public int Count(string sqlText)
        {
            int result = 0;
            try
            {
                //打开数据库连接
                conn.Open();
                //建立连接,并执行sql语句命令
                SqlCommand command = new SqlCommand(sqlText, conn);
                result = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception)
            {
                
            }
            finally 
            {
                //关闭数据库连接
                conn.Close();
            }
            return result;
        }
    }
}

 

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