• 自己写的数据库访问组件 使用ADO.NET 比较简单


        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

  • 相关阅读:
    Git修改注释
    数组和切片的区别
    SpringBoot启动报:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    eclipse中web.xml报:The content of element type "web-app" must match "(icon?,display- name?,
    eclipse错误: 在类xxx中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
    idea Cannot resolve method ‘setAttribute(java.lang.String, java.lang.String)/不能使用pageContext.setAttribute()
    解决Nginx启动报nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    数据仓库开发规范
    详解会话技术cookie、session和token
    Requests爬虫包及解析工具 xpath、正则、Beautiful Soup
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1710691.html
Copyright © 2020-2023  润新知