.NET关于数据库操作的类-囊括所有的操作

   本人目前在实习,还没有毕业,哈哈------还是一小弟。之前在学校中做了些小的项目,自己总结的关于数据库操作的类,对于一些小的项目还是很有帮助的,现在一直在做silverlight的项目,目前连数据库长什么样都没有见过-----服务器不是偶写的,因为刚来也没好意思问。

初来驾到,又如bug还请多多指教。

先贴代码吧。

using System.Data;
using System.Data.SqlClient;
namespace D.King.DB
{
    public class DBOperator
    {
        private string connStr;

        private SqlConnection conn;

        public delegate void getParams(SqlCommand comm);

        public delegate void getReader(IDataReader dataReader);
		
        public DBOperator()
        {
			//连接字符串自己看着办
            connStr ="Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=BaseStation3;Data Source=.";
            conn = new SqlConnection(connStr);
        }

        public DBOperator(string connStr)
        {
            this.connStr = connStr;
            conn = new SqlConnection(connStr);
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        private void openConn()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            else
            {
                conn.Close();
                conn.Open();
            }
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        private void closeConn()
        {
            if (conn.State != ConnectionState.Closed)
                conn.Close();
        }

        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public string ConnString
        {
            get
            {
                return connStr;
            }
        }

        /// <summary>
        /// 获得查询的表
        /// </summary>
        /// <param name="sql">TSQL或存储过程</param>
        /// <param name="getparam">参数添加方法</param>
        /// <returns>DataTable</returns>
        public DataTable getTable(string sql, getParams getparam)
        {
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter();
            if (getparam != null)
            {
                getparam(comm);
            }
            da.SelectCommand = comm;
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 获得查询的数据集
        /// </summary>
        /// <param name="sql">TSQL或存储过程</param>
        /// <param name="getparam">参数添加方法</param>
        /// <returns>DataSet</returns>
        public DataSet getDataSet(string sql, getParams getparam)
        {
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter();
            if (getparam != null)
            {
                getparam(comm);
            }
            da.SelectCommand = comm;
            DataSet dt = new DataSet();
            da.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 执行TSQL或存储过程
        /// </summary>
        /// <param name="sql">TSQL或存储过程</param>
        /// <param name="getparam">参数添加方法</param>
        /// <returns>影响的行数</returns>
        public int Execute(string sql, getParams getparam)
        {
            int result = 0;
            SqlCommand comm = new SqlCommand(sql, conn);
            if (getparam != null)
            {
                getparam(comm);
            }
            openConn();
            result = comm.ExecuteNonQuery();
            closeConn();
            return result;
        }

        /// <summary>
        /// 执行dataReader绑定数据
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="reader">reader绑定方法</param>
        public void GetDataReader(string sql,getReader reader)
        {
            SqlCommand comm = new SqlCommand(sql,conn);
            openConn();
            if (reader != null)
            {
                reader(comm.ExecuteReader());
            }
            closeConn();
        }

        /// <summary>
        /// 执行dataReader绑定数据
        /// </summary>
        /// <param name="sql">查询语句或存储过程</param>
        /// <param name="getparam">存储过程参数绑定</param>
        /// <param name="reader">reader绑定方法</param>
        public void GetDataReader(string sql,getParams getparam, getReader reader)
        {
            SqlCommand comm = new SqlCommand(sql, conn);
            if (getparam != null)
                getparam(comm);
            openConn();
            if (reader != null)
            {
                reader(comm.ExecuteReader());
            }
            closeConn();
        }
    }
}


下面是关于如何使用这个类。

1.读取数据

DBOperator db=new DBOperator();

DataTable dt=db.getTable("select * from table1",null);

或者:dt=db.getTable("select * from table1 where id=@id",setPama);

现在需要一个setParam的定义如下:

private void setPama(SqlCommand comm)

{

comm.Parameters.AddWithValue("@id",id);

//后面的那个id当然是一个变量了,@id是上面select语句中的一个临时变量。

2.数据操作

int num=db.Execute("insert into table1 (id,name) values(@id,@name)",setPama);//num的值就是数据库操作后返回的影响的行数,可以通过num的值判断是否修改成功。

private void setPama(SqlCommand comm)

{

comm.Parameters.AddWithValue("@id",id);//这个和读取数据中的意思差不多

comm.Parameters.AddWithValue("@name",name);//一共定义了两个变量,所以就两句咯

3.调用存储过程

int num =db.db.Execute("pro_test",setPama);//pro_test这个是数据库中存储过程的名字,setPama还是为存储过程的参数设置值,第二个参数是不允许为空的----存储过程的内容就不贴出来了

private void setPama(SqlCommand comm)

{

comm.Parameters.AddWithValue("@id",id);//这个@id是数据库中的存储过程中定义的参数的名称。

comm.CommandType = CommandType.StoredProcedure;//这一句可是说明使用的是存储过程,这也是为什么上面提到的第二个参数不能为空的原因。

4.DataReader的我就不贴代码了。

几乎所有的数据库操作都包含在内了,对于小型的项目个人感觉还是比较好了。

对于设计模式,我了解的比较少,现在刚刚入门了一个MVVM,不知道在大型的项目中这个东西还好不好使。。。。

posted @ 2010-12-09 21:52  Honker Snow  阅读(425)  评论(0编辑  收藏  举报