Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108523
  • 博文数量: 30
  • 博客积分: 2051
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-26 08:31
文章分类

全部博文(30)

文章存档

2017年(2)

2016年(1)

2014年(1)

2011年(2)

2009年(24)

我的朋友

分类:

2009-01-04 16:59:52

//modify by mfq 4.14 22:50 line7
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace My.Hr.DbBase
{
    ///
    /// base.
    ///

    public abstract class Base
    {

        #region "Fields of base calss"

        ///
        /// connecting to Database
        ///

        protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];

        ///
        /// SQL command
        ///

        protected static string strSQL;

        #endregion
        

        #region "Properties of base class"
        private int m_ID;
        private string m_Name;

        ///
        /// Property:ID
        ///

        public int ID
        {
            get
            {
                return m_ID;
            }
            set
            {
                m_ID = value;
            }
        }


        ///
        /// name
        ///

        public string Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
            }
        }

        #endregion


        #region "Functions of base class"
        public Base()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        ///
        /// executing SQL commands
        ///

        /// string
        /// return int
        protected static int ExecuteSql(string strSQL)
        {
            SqlConnection myCn = new SqlConnection(strConn);            
            SqlCommand myCmd = new SqlCommand(strSQL,myCn);
            try
            {
                myCn.Open();
                myCmd.ExecuteNonQuery();
                return 0;
            }
            catch(System.Data.SqlClient.SqlException e)
            {                
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        ///executing SQL commands
        ///

        /// 要执行的SQL语句,为字符串类型string
        /// 返回执行情况,整形int
        protected static int ExecuteSqlEx(string strSQL)
        {
            SqlConnection myCn = new SqlConnection(strConn);            
            SqlCommand myCmd = new SqlCommand(strSQL,myCn);
            
            try
            {
                myCn.Open();
                //只能这样创建一个SqlDataReader,因为该类没有公共的构造函数mfq                
                SqlDataReader myReader = myCmd.ExecuteReader();
                //只调用一次,只能返回一行数据记录
                if(myReader.Read())
                {
                    return 0;
                }    
                else
                {
                    throw new Exception("Value Unavailable!");
                }
            }
            catch(System.Data.SqlClient.SqlException e)
            {                                
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        /// get dataset
        ///

        /// (string)
        /// (DataSet)
        protected static DataSet ExecuteSql4Ds(string strSQL)
        {
            SqlConnection myCn = new SqlConnection(strConn);            
            try
            {
                myCn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn);
                DataSet ds = new DataSet("ds");
                sda.Fill(ds);
                return ds;
            }
            catch(System.Data.SqlClient.SqlException e)
            {                
                throw new Exception(e.Message);
            }
            finally
            {
                myCn.Close();
            }
        }


        ///
        /// get single value
        ///

        /// (string)
        /// (int)
        protected static int ExecuteSql4Value(string strSQL)
        {
            SqlConnection myCn = new SqlConnection(strConn);            
            SqlCommand myCmd = new SqlCommand(strSQL,myCn);
            try
            {
                myCn.Open();
                object r = myCmd.ExecuteScalar();
                if(Object.Equals(r,null))
                {
                    throw new Exception("value unavailable!");
                }
                else
                {
                    return (int)r;
                }                
            }
            catch(System.Data.SqlClient.SqlException e)
            {                
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }        


        ///
        /// get object
        ///

        /// (string)
        /// (object)
        protected static object ExecuteSql4ValueEx(string strSQL)
        {
            SqlConnection myCn = new SqlConnection(strConn);            
            SqlCommand myCmd = new SqlCommand(strSQL,myCn);
            try
            {
                myCn.Open();
                object r = myCmd.ExecuteScalar();
                if(Object.Equals(r,null))
                {
                    throw new Exception("object unavailable!");
                }
                else
                {
                    return r;
                }                
            }
            catch(System.Data.SqlClient.SqlException e)
            {                
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        /// execute multipul SQL commands 
        ///

        /// string
        /// int
        protected static int ExecuteSqls(string[] strSQLs)
        {
            SqlConnection myCn = new SqlConnection(strConn);            
            SqlCommand myCmd = new SqlCommand();            
            int j=strSQLs.Length;

            try
            {
                myCn.Open();                
            }
            catch(System.Data.SqlClient.SqlException e)
            {
                throw new Exception(e.Message);
            }
            SqlTransaction myTrans = myCn.BeginTransaction();

            try
            {                                            
                myCmd.Connection = myCn;                
                myCmd.Transaction = myTrans;

                foreach(string str in strSQLs)
                {
                    myCmd.CommandText = str;
                    myCmd.ExecuteNonQuery();
                }
                myTrans.Commit();
                return 0;
            }
            catch(System.Data.SqlClient.SqlException e)
            {            
                myTrans.Rollback();
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }

        #endregion
    }
}
阅读(1195) | 评论(0) | 转发(0) |
0

上一篇:连接access底层文件

下一篇:php+access类

给主人留下些什么吧!~~