Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19001806
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: 数据库开发技术

2008-05-28 09:59:31

由于自己工作需要 经常重复写数据库的操作代码.今天我把他们写成了dll, 拿来和大家分享.希望大家提出意见.支持事务,目前还不支持所有存储过程.

DataMsSql.cs

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

namespace DataOperation
{
    ///


    /// MsSql操作类
    ///

    sealed public class DataMsSql
    {
        private SqlConnection dataConnection = null;

        #region 构造函数
        ///


        /// 无参构造函数
        ///

        public DataMsSql()
        {
            dataConnection = new SqlConnection();
        }
        ///
        /// 有参构造函数
        ///

        /// 数据库联接串
        public DataMsSql(string connectionString)
        {
            dataConnection = new SqlConnection(connectionString);
        }
        #endregion

        #region 属性 
        ///


        /// 设置或获取SqlConnection类型数据库联接dataConnection的值
        ///

        public SqlConnection DataConnection
        {
            get
            {
                if (dataConnection == null)
                    dataConnection = new SqlConnection();
                return dataConnection;
            }
            set
            {
                dataConnection = value;
            }
        }
        ///
        /// 获取数据库联接的状态
        ///

        public ConnectionState SqlState
        {
            get
            {
                return dataConnection.State;
            }
        }
        #endregion

        #region 状态操作
        ///


        /// 打开数据库联接
        ///

        public void Open()
        {
            if (dataConnection.State != ConnectionState.Open)
                dataConnection.Open();
        }
        ///
        /// 关闭数据库联接
        ///

        public void Close()
        {
            if (dataConnection.State != ConnectionState.Closed)
                dataConnection.Close();
        }
        #endregion

        #region 一般数据语句操作
        ///


       /// 获取检索出来首行首列的值
       ///

       /// sql查询串
       /// 结果
        public object ExecuteScalar(string sqlStr)
        {
            if (SqlState == ConnectionState.Closed)
                this.Open();
            SqlCommand cmd = new SqlCommand(sqlStr, dataConnection);
            return cmd.ExecuteScalar();
        }
        ///
        ///检索数据以SqlDataReader形式返检索结果
        ///

        /// sql查询串
        /// SqlDataReader数据集
        public SqlDataReader GetDataReadr(string sqlStr)
        {
            if (SqlState == ConnectionState.Closed)
                this.Open();
            SqlCommand cmd = new SqlCommand(sqlStr, dataConnection);
            return cmd.ExecuteReader();
        }
        ///
        /// 检索数据以DataTable形式返检索结果
        ///

        /// sql查询串
        /// DataTale数据集
        public DataTable GetDataTable(string sqlStr)
        {
            DataTable dt = new  DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, dataConnection);
            adapter.Fill(dt);
            return dt;
        }
        ///
        /// 执行出检索以外的其它数据操作返回影响条数
        ///

        /// sql操作语句
        /// 操作影响条数
        public int ExecuteSql(String sqlStr)
        {
            if (SqlState == ConnectionState.Closed)
                this.Open();
            SqlCommand cmd = new SqlCommand(sqlStr, DataConnection);
            return cmd.ExecuteNonQuery();
        }
        #endregion

        #region 存储过程操作
       ///


       /// 执行存储过程获得DataTable数据
       ///

        /// 存储过程参数ArrayList类型的SqlParameter集合
       /// 存储过程名
       ///
        public DataTable ProGetDataTable(ArrayList sqlParameters, string storedProcedureName)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = dataConnection;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = storedProcedureName;
            if(sqlParameters != null && sqlParameters.Count != 0)
                foreach(object parameter in sqlParameters)
                {
                    cmd.Parameters.Add((SqlParameter)parameter);
                }
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            dataAdapter.Fill(dt);
            return dt;
        }
        #endregion

        #region 事务操作
        ///


        /// 开启事务
        ///

        public void BeginTransaction()
        {
            ExecuteSql("begin transaction;");
        }
        ///
        /// 提交事务
        ///

        public void Commit()
        {
            ExecuteSql("commit;");
        }
        ///
        /// 回滚事务
        ///

        public void Rollback()
        {
            ExecuteSql("rollback;");
        }
        #endregion

    }
}

有要已编译成dll的 留邮箱

阅读(477) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~