简单的3层架构底层书写

来源:互联网 发布:sql替换某个字符 编辑:程序博客网 时间:2024/05/16 18:38

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

namespace HospitalSystem
{

    class DataSource
    {
       
        #region 自定义变量
        public SqlCommand mCommand = new SqlCommand();
        public SqlConnection mConnection = new SqlConnection();
        //private SqlConnection Connection
        //{
        //    get
        //    {
        //        if (this.mCommand != null)
        //        {
        //            return mCommand.Connection;
        //        }
        //        else
        //        {
        //            return null;
        //        }
        //    }
        //    set
        //    {
        //        if (mCommand != null)
        //        {
        //            mCommand.Connection = value;
        //        }
               
        //    }
        //}
        #endregion


        #region 连接字符串
        public string ConnString(string IpAddress, string DBName, string UserName, string UserPwd)
        {
            string pConnectStr = "";
            pConnectStr = "user " + "id=" + UserName + ";" + "password=" + UserPwd + ";"
                + "initial catalog=" + DBName + ";"
                + "data source=" + IpAddress + ";"
                + "Connect Timeout=" + "30";
            return pConnectStr;
        }
        public void ConnectDataBase()
        {
            if (mConnection.State.ToString() == "open" && mConnection != null)
            {
                try
                {
                    mConnection.Close();
                    mConnection = null;
                }
                catch (Exception ex)
                {
                    throw(ex);
                }
            }
            string mDBNmae =  ConfigurationSettings.AppSettings["DBName"];
            string mIpAddress = ConfigurationSettings.AppSettings["IpAddress"];
            string mUserName = ConfigurationSettings.AppSettings["UserName"];
            string mUserPwd = ConfigurationSettings.AppSettings["UserPwd"];
            mConnection.ConnectionString = ConnString(mIpAddress, mDBNmae, mUserName, mUserPwd);
        }
        #endregion
        #region command方法
        public void CommandConnDatabase()
        {
            this.ConnectDataBase();
            mCommand = mConnection.CreateCommand();
        }
        public int ExecuteNonQuery()
        {
            int mValue = -1;
            try
            {
                if (mCommand.Connection.State.ToString() == "close")
                {
                    mCommand.Connection.Open();
                }
                mCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (mCommand.Connection.State.ToString() == "open")
                {
                    mCommand.Connection.Close();
                }
            }
            return mValue;
        }
        public SqlDataReader ExecuteReader()
        {
            SqlDataReader aDateReader = null;
            try
            {
                mCommand.Connection.Open();

                aDateReader = mCommand.ExecuteReader();
            }
            catch (Exception err)
            {
                throw (err);
            }
            finally
            {
                if (mCommand.Connection.State.ToString() == "Open")
                {
                    mCommand.Connection.Close();
                }
            }
            return aDateReader;

        }

        public object ExecuteScalar()
        {
            object aObject = null;
            try
            {
                mCommand.Connection.Open();

                aObject = mCommand.ExecuteScalar();
            }
            catch (Exception err)
            {
                throw (err);
            }
            finally
            {
                if (mCommand.Connection.State.ToString() == "Open")
                {
                    mCommand.Connection.Close();
                }
            }


            return aObject;
        }
        #endregion
        /// <summary>
        /// 数据适配器,用于填充Dataset,DataTable.用时,需要将selectcommand赋值.
        /// </summary>
        public  SqlDataAdapter mAdapter = new SqlDataAdapter();

        public  SqlDataAdapter Adapter
        {
            get
            {
                return mAdapter;
            }
            set
            {
                mAdapter = value;
            }
        }

    }

}

原创粉丝点击