自己寫的資料庫訪問組件 使用ADO.NET 比較簡單

來源:互聯網
上載者:User
    Model    資料庫模型,封裝資料庫中的表

    例如:Person表,

代碼        /// <summary>
    /// 實體類Person 。(屬性說明自動提取資料庫欄位的描述資訊)
    /// </summary>
    [Serializable]
    public class Person
    {
        public Person()
        {}
        #region Model
        private int _personid;
        private string _lastname;
        private string _firstname;
        private int _personcategory;
        private DateTime? _hiredate;
        private DateTime? _enrollmentdate;
        private byte[] _picture;
        private string password;
        /// <summary>
        /// 
        /// </summary>
        public int PersonID
        {
            set{ _personid=value;}
            get{return _personid;}
        }
        /// <summary>
        /// 
        /// </summary>
        public string LastName
        {
            set{ _lastname=value;}
            get{return _lastname;}
        }
        /// <summary>
        /// 
        /// </summary>
        public string FirstName
        {
            set{ _firstname=value;}
            get{return _firstname;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int PersonCategory
        {
            set{ _personcategory=value;}
            get{return _personcategory;}
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? HireDate
        {
            set{ _hiredate=value;}
            get{return _hiredate;}
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? EnrollmentDate
        {
            set{ _enrollmentdate=value;}
            get{return _enrollmentdate;}
        }
        /// <summary>
        /// 
        /// </summary>
        public byte[] Picture
        {
            set{ _picture=value;}
            get{return _picture;}
        }

        public string Password
        {
            get { return password; }
            set { password=value;}
        }
        #endregion Model

    }

 

 DBAccess

資料庫訪問層,用ADO.NET 提供訪問資料庫的借口,包括各種資料庫的提供者:,使用了簡單工廠設計模式。

介面:ISQLHelper

 代碼

SQLServer協助類:DbHelperSQL

 代碼

建立協助類的工廠:SQLHelperFactory,根據設定檔,初始化不同的資料庫協助類

代碼    /// <summary>
    /// 建立協助類的工廠
    /// </summary>
    public class SQLHelperFactory
    {
        public static ISQLHelper GetHelper()
        {
            switch(PubConstant.DBTypeString)
            {
                case "MSSQL":
                    return new DbHelperSQL(PubConstant.ConnectionString);
                case "Oracle":
                    return new DbHelperOra(PubConstant.ConnectionString);
                case "Oledb":
                    return new DbHelperOleDb(PubConstant.ConnectionString);
                default:
                    throw new Exception("請確保設定檔的真確性!");
            }
        }
    }

 

  

    DataAccessLayer 

 具體對象的資料庫訪問抽象類別,基礎類

 

代碼    public class DataAccessBase
    {
        protected ISQLHelper DbHelperSQL
        {
            get
            {
                return SQLHelperFactory.GetHelper();
            }

        }
        public DataAccessBase()
        { }
    }

 

 Person對象資料庫訪問類

代碼        /// <summary>
    /// 資料訪問類PersonDataAccess
    /// </summary>
    public class PersonDataAccess:DataAccessBase
    {
        public PersonDataAccess()
        {}
        #region  成員方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
        return DbHelperSQL.GetMaxID("PersonID", "Person"); 
        }

 

 BussinessIntegrateLayer

邏輯訪問層,即對外界的提供者,外部直接此組件中的Service方法就可以了。

 

 代碼

以上只是部分代碼,源碼如下,並附有資料庫建庫指令碼,希望廣大網友斧正。

 /Files/csharponworking/Model.rar

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.