• CodeSmith DAL生成模板


    前面有贴出来一个CodeSmith  Model生成模板的文章,现把DAL生成模板也贴出来。这里的数据库是用Northwind。表是用Customers。 

    CodeSmith Dal Code下载地址:CodeSmith Dal 下载 

    CodeSmith Model Code下载地址:CodeSmith Model下载

     命名空间名称 NameSpace:Chhuic

    DAL类名称 ClassName:Dal.CustomersDAL

    Model名称 ModelName:Model.CustomersModel

    表 SourceTable:dbo.Customers

    数据库操作基础类 DBFactoryName:SqlServerBase 指使用的数据库操作基础类的名称

    数据库表参数名称 ParameterType:SqlParameter  如果是Oracle则写OracleParameter 

    生成后的CustomersDAL.CS为:

    代码

    ///////////////////////////////////////////////////////////////////////////////////////
    // File: CustomersDAL.cs
    // Description: the CustomersDAL Created By CodeSmith .
    // ---------------------
    // Copyright: @ 2010-1-5 10:08:50 
    // ---------------------
    // History:
    ///////////////////////////////////////////////////////////////////////////////////////
     
    using System;

    using Chhuic.Model.CustomersModel;
     
    namespace Chhuic.Dal.CustomersDAL
    {
       
    /// <summary>
       
    /// Summary description for CustomersModel. 
       
    /// Description:
       
    /// </summary>
       public class CustomersDAL
       {
            
    #region Constructor
            
    /// <summary>
            
    /// CustomersDAL Constructor
            
    /// </summary>
            public CustomersDAL()
            {
            }
            
            
    #endregion
            
            
    #region GetMaxID
            
    /// <summary>
            
    /// get the max PrimaryKey ID value
            
    /// </summary>
            
    /// <return>int<return>
            public int GetMaxID()
            {
                
    string sql="select Max(CustomerID)+1 from Customers";
                
    try
                {
                    
    return int.Parse(SqlServerBase.ExecuteScalar(sql).ToString());
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    #endregion
            
            
    #region Exist
            
    /// <summary>
            
    /// check the record is Exist by the  CustomerID value
            
    /// </summary>
            
    /// <param name="CustomerID">CustomerID</param>
            
    /// <return>bool<return>
            public bool Exist(int CustomerID)
            {
                
    string sql="select Count(*) from Customers where CustomerID=@CustomerID";
                
    try
                {
                    
    object result = SqlServerBase.ExecuteScalar(sql,new SqlParameter[]{new SqlParameter("@CustomerID",CustomerID)});
                    
    if(result == null||int.Parse(result.ToString()) == 0)
                    {
                        
    return false;
                    }
                    
    return true;
                }
                
    catch(Exception ex)
                {
                }
                
    return false;
            }
            
    /// <summary>
            
    /// check the record is Exist by the filter value
            
    /// </summary>
            
    /// <param name="filter">filter</param>
            
    /// <return>bool<return>
            public bool Exist(string filter)
            {
                
    string sql="select Count(*) from Customers " ;
                
    if(!string.IsNullOrEmpty(filter))
                {
                    sql
    +=" where "+filter;
                }
                
    try
                {
                    
    object result=SqlServerBase.ExecuteScalar(sql);
                    
    if(result == null||int.Parse(result.ToString())==0)
                    {
                        
    return false;
                    }
                    
    return true;
                }
                
    catch(Exception ex)
                {
                }
                
    return false;
            }
            
            
    #endregion
            
            
    #region Add
            
    /// <summary>
            
    /// Add
            
    /// </summary>
            
    /// <param name="CustomersModel">CustomersModel</param>
            
    /// <return>int<return>
            public int Add(Model.CustomersModel.CustomersModel model)
            {
                
    string sql="insert into Customers(CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax) values(@CompanyName,@ContactName,@ContactTitle,@Address,@City,@Region,@PostalCode,@Country,@Phone,@Fax);select @@IDENTITY; ";
                SqlParameter[] parameters
    ={
                    
    new SqlParameter("@CompanyName",DbType.String),
                    
    new SqlParameter("@ContactName",DbType.String),
                    
    new SqlParameter("@ContactTitle",DbType.String),
                    
    new SqlParameter("@Address",DbType.String),
                    
    new SqlParameter("@City",DbType.String),
                    
    new SqlParameter("@Region",DbType.String),
                    
    new SqlParameter("@PostalCode",DbType.String),
                    
    new SqlParameter("@Country",DbType.String),
                    
    new SqlParameter("@Phone",DbType.String),
                    
    new SqlParameter("@Fax",DbType.String)
                };
                parameters[
    0].Value=model.CompanyName;
                parameters[
    1].Value=model.ContactName;
                parameters[
    2].Value=model.ContactTitle;
                parameters[
    3].Value=model.Address;
                parameters[
    4].Value=model.City;
                parameters[
    5].Value=model.Region;
                parameters[
    6].Value=model.PostalCode;
                parameters[
    7].Value=model.Country;
                parameters[
    8].Value=model.Phone;
                parameters[
    9].Value=model.Fax;
                
    try
                {
                    
    int result = int.Parse(SqlServerBase.ExecuteScalar(sql,parameters).ToString());
                    
    return result;
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    #endregion
            
            
    #region Update
            
    /// <Summary>
            
    /// Update Customers table by Model.CustomersModel.CustomersModel
            
    /// <Summary>
            
    /// <param name="CustomersModel">CustomersModel</param>
            
    /// <return>int</return>
            public int Update(Model.CustomersModel.CustomersModel model)
            {
                
    string sql="update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax where CustomerID= "+model.CustomerID;
                SqlParameter[] parameters
    ={
                    
    new SqlParameter("@CompanyName",DbType.String),
                    
    new SqlParameter("@ContactName",DbType.String),
                    
    new SqlParameter("@ContactTitle",DbType.String),
                    
    new SqlParameter("@Address",DbType.String),
                    
    new SqlParameter("@City",DbType.String),
                    
    new SqlParameter("@Region",DbType.String),
                    
    new SqlParameter("@PostalCode",DbType.String),
                    
    new SqlParameter("@Country",DbType.String),
                    
    new SqlParameter("@Phone",DbType.String),
                    
    new SqlParameter("@Fax",DbType.String)
                };
                parameters[
    0].Value=model.CompanyName;
                parameters[
    1].Value=model.ContactName;
                parameters[
    2].Value=model.ContactTitle;
                parameters[
    3].Value=model.Address;
                parameters[
    4].Value=model.City;
                parameters[
    5].Value=model.Region;
                parameters[
    6].Value=model.PostalCode;
                parameters[
    7].Value=model.Country;
                parameters[
    8].Value=model.Phone;
                parameters[
    9].Value=model.Fax;
                
    try
                {
                    
    int result = int.Parse(SqlServerBase.ExecuteNonQuery(sql,parameters).ToString());
                    
    return result;
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    /// <Summary>
            
    /// Update the Customers table by the CustomerID value
            
    /// <Summary>
            
    /// <param name="CustomersModel">CustomersModel</param>
            
    /// <param name="CustomerID">CustomerID</param>
            
    /// <return>int</return>
            public int Update(Model.CustomersModel.CustomersModel model,int CustomerID)
            {
                
    string sql="update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax where CustomerID="+CustomerID ;
                SqlParameter[] parameters
    ={
                    
    new SqlParameter("@CompanyName",DbType.String),
                    
    new SqlParameter("@ContactName",DbType.String),
                    
    new SqlParameter("@ContactTitle",DbType.String),
                    
    new SqlParameter("@Address",DbType.String),
                    
    new SqlParameter("@City",DbType.String),
                    
    new SqlParameter("@Region",DbType.String),
                    
    new SqlParameter("@PostalCode",DbType.String),
                    
    new SqlParameter("@Country",DbType.String),
                    
    new SqlParameter("@Phone",DbType.String),
                    
    new SqlParameter("@Fax",DbType.String)
                };
                parameters[
    0].Value=model.CompanyName;
                parameters[
    1].Value=model.ContactName;
                parameters[
    2].Value=model.ContactTitle;
                parameters[
    3].Value=model.Address;
                parameters[
    4].Value=model.City;
                parameters[
    5].Value=model.Region;
                parameters[
    6].Value=model.PostalCode;
                parameters[
    7].Value=model.Country;
                parameters[
    8].Value=model.Phone;
                parameters[
    9].Value=model.Fax;
                
    try
                {
                    
    int result = int.Parse(SqlServerBase.ExecuteNonQuery(sql,parameters).ToString());
                    
    return result;
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
            
    /// <summary>
            
    /// Update Customers table by the  filter value
            
    /// </summary>
            
    /// <param name="CustomersModel">CustomersModel</param>
            
    /// <param name="filter">filter</param>
            
    /// <return>int</return>
            public int Update(Model.CustomersModel.CustomersModel model,string filter)
            {
                
    string sql="update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax where "+filter;
                SqlParameter[] parameters
    ={
                    
    new SqlParameter("@CompanyName",DbType.String),
                    
    new SqlParameter("@ContactName",DbType.String),
                    
    new SqlParameter("@ContactTitle",DbType.String),
                    
    new SqlParameter("@Address",DbType.String),
                    
    new SqlParameter("@City",DbType.String),
                    
    new SqlParameter("@Region",DbType.String),
                    
    new SqlParameter("@PostalCode",DbType.String),
                    
    new SqlParameter("@Country",DbType.String),
                    
    new SqlParameter("@Phone",DbType.String),
                    
    new SqlParameter("@Fax",DbType.String)
                };
                parameters[
    0].Value=model.CompanyName;
                parameters[
    1].Value=model.ContactName;
                parameters[
    2].Value=model.ContactTitle;
                parameters[
    3].Value=model.Address;
                parameters[
    4].Value=model.City;
                parameters[
    5].Value=model.Region;
                parameters[
    6].Value=model.PostalCode;
                parameters[
    7].Value=model.Country;
                parameters[
    8].Value=model.Phone;
                parameters[
    9].Value=model.Fax;
                
    try
                {
                    
    int result = int.Parse(SqlServerBase.ExecuteNonQuery(sql,parameters).ToString());
                    
    return result;
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    #endregion
            
            
    #region Delete
            
    /// <summary>
            
    /// Delete the Customers all record.
            
    /// </summary>
            
    /// <return>int<return>
            public int Delete()
            {
                
    string sql="delete from Customers ";
                
    try
                {
                    
    return SqlServerBase.ExecuteNonQuery(sql);
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    /// <summary>
            
    /// Delete the Customers table record  by the CustomerID value
            
    /// </summary>
            
    /// <param name="CustomerID">CustomerID</param>
            
    /// <return>int<return>
            public int Delete(int CustomerID)
            {
                
    string sql="delete from Customers where CustomerID="+CustomerID;
                
    try
                {
                    
    return SqlServerBase.ExecuteNonQuery(sql);
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    /// <summary>
            
    /// Delete the Customers table record  by the filter value
            
    /// </summary>
            
    /// <param name="filter">filter</param>
            
    /// <return>int<return>
            public int Delete(string filter)
            {
                
    string sql="delete from Customers where "+filter;
                
    try
                {
                    
    return SqlServerBase.ExecuteNonQuery(sql);
                }
                
    catch(Exception ex)
                {
                }
                
    return -1;
            }
            
    #endregion
            
            
    #region DataSet
            
    /// <summary>
            
    /// get DataSet records
            
    /// </summary>
            
    /// <return>DataSet<return>
            public DataSet GetList()
            {
                
    string sql="select * from Customers ";
                
    try
                {
                    
    return SqlServerBase.ExecuteDataset(sql);
                }
                
    catch(Exception ex)
                {
                }
                
    return null;
            }
            
    /// <summary>
            
    /// get DataSet records by CustomerID value
            
    /// </summary>
            
    /// <param name="CustomerID">CustomerID</param>
            
    /// <return>DataSet<return>
            public DataSet GetList(int CustomerID)
            {
                
    string sql="select * from Customers where CustomerID="+CustomerID;
                
    try
                {
                    
    return SqlServerBase.ExecuteDataset(sql);
                }
                
    catch(Exception ex)
                {
                }
                
    return null;
            }
            
    /// <summary>
            
    /// get DataSet records by the filter value
            
    /// </summary>
            
    /// <param name="filter">filter</param>
            
    /// <return>DataSet<return>
            public DataSet GetList(string filter)
            {
                
    string sql="select * from Customers where "+filter;
                
    try
                {
                    
    return SqlServerBase.ExecuteDataset(sql);
                }
                
    catch(Exception ex)
                {
                }
                
    return null;
            }
            
    #endregion
            
            
    #region Query
            
    /// <summary>
            
    /// Query  by the  sql
            
    /// </summary>
            
    /// <param name="sql">sql</param>
            
    /// <return>DataSet<return>
            public DataSet Query(string sql)
            {
                
    if(string.IsNullOrEmpty(sql))
                {
                    
    return null;
                }
                
    try
                {
                    
    return SqlServerBase.ExecuteDataset(sql);
                }
                
    catch
                {
                }
                
    return null;
            }
            
    #endregion
            
            
    #region Model.CustomersModel.CustomersModel
            
    /// <summary>
            
    /// get Model.CustomersModel.CustomersModel by the CustomerID value
            
    /// </summary>
            
    /// <param name="CustomerID">CustomerID</param>
            
    /// <return>CustomersModel<return>
            public Model.CustomersModel.CustomersModel GetModel(int CustomerID)
            {
                
    try
                {
                    DataSet ds 
    = GetList(CustomerID);
                    
    if(ds==null||ds.Tables.Count==0||ds.Tables[0].Rows.Count==0)
                    {
                        
    return null;
                    }
                    
    return GetModel(ds.Tables[0].Rows[0]);
                }
                
    catch
                {
                }
                
    return null;
            }
            
    /// <summary>
            
    /// get Model.CustomersModel.CustomersModels 
            
    /// </summary>
            
    /// <return>CustomersModel<return>
            public Model.CustomersModel.CustomersModel[] GetModels()
            {
                
    try
                {
                    DataSet ds 
    = GetList();
                    
    if(ds==null||ds.Tables.Count==0||ds.Tables[0].Rows.Count==0)
                    {
                        
    return null;
                    }
                    
    return GetModels(ds.Tables[0]);
                }
                
    catch
                {
                }
                
    return null;
            }
            
    /// <summary>
            
    /// get Model.CustomersModel.CustomersModel by the filter value
            
    /// </summary>
            
    /// <param name="filter">filter</param>
            
    /// <return>CustomersModel<return>
            public Model.CustomersModel.CustomersModel GetModels(string filter)
            {
                
    try
                {
                    DataSet ds 
    = GetList(filter);
                    
    if(ds==null||ds.Tables.Count==0||ds.Tables[0].Rows.Count==0)
                    {
                        
    return null;
                    }
                    
    return GetModel(ds.Tables[0].Rows[0]);
                }
                
    catch
                {
                }
                
    return null;
            }
            
    /// <summary>
            
    /// get Model.CustomersModel.CustomersModel by the datarow value
            
    /// </summary>
            
    /// <param name="dr">dr</param>
            
    /// <return>CustomersModel<return>
            public Model.CustomersModel.CustomersModel GetModel(DataRow dr)
            {
                
    if(dr==null)
                {
                    
    return null;
                }
                Model.CustomersModel.CustomersModel model
    =new Model.CustomersModel.CustomersModel();
                
                
    if(!string.IsNullOrEmpty(dr["CustomerID"].ToString()))
                {
                    model.CustomerID
    =dr["CustomerID"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["CompanyName"].ToString()))
                {
                    model.CompanyName
    =dr["CompanyName"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["ContactName"].ToString()))
                {
                    model.ContactName
    =dr["ContactName"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["ContactTitle"].ToString()))
                {
                    model.ContactTitle
    =dr["ContactTitle"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["Address"].ToString()))
                {
                    model.Address
    =dr["Address"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["City"].ToString()))
                {
                    model.City
    =dr["City"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["Region"].ToString()))
                {
                    model.Region
    =dr["Region"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["PostalCode"].ToString()))
                {
                    model.PostalCode
    =dr["PostalCode"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["Country"].ToString()))
                {
                    model.Country
    =dr["Country"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["Phone"].ToString()))
                {
                    model.Phone
    =dr["Phone"].ToString();
                }
                
    if(!string.IsNullOrEmpty(dr["Fax"].ToString()))
                {
                    model.Fax
    =dr["Fax"].ToString();
                }
                
    return model;
            }
            
    /// <summary>
            
    /// get Model.CustomersModel.CustomersModel by the datatable records
            
    /// </summary>
            
    /// <param name="dt">dt</param>
            
    /// <return>CustomersModel<return>
            public Model.CustomersModel.CustomersModel[] GetModels(DataTable dt)
            {
                
    int i=0;
                Model.CustomersModel.CustomersModel[] models
    =new Model.CustomersModel.CustomersModel[dt.Rows.Count];
                
    foreach(DataRow dr in dt.Rows)
                {
                    models[i]
    =GetModel(dr);
                    i
    ++;
                }
                
    return models;
            }
            
            
    #endregion
       }
    }

    CustomersModel.cs

    代码
    ///////////////////////////////////////////////////////////////////////////////////////
    // File: CustomersModel.cs
    // Description: the CustomersModel Created By CodeSmith.
    // ---------------------
    // Copyright: @ 2010-1-5 10:24:46 
    // ---------------------
    // History:
    ///////////////////////////////////////////////////////////////////////////////////////
     
    using System;
     
    namespace Chhuic.Model.CustomersModel
    {
       
    /// <summary>
       
    /// Summary description for CustomersModel. 
       
    /// Description:
       
    /// </summary>
       public class CustomersModel
       {
            
    #region Private Protery
            
            
    private string   _customerID;
            
    private string   _companyName;
            
    private string   _contactName;
            
    private string   _contactTitle;
            
    private string   _address;
            
    private string   _city;
            
    private string   _region;
            
    private string   _postalCode;
            
    private string   _country;
            
    private string   _phone;
            
    private string   _fax;
            
            
    #endregion
            
            
    #region Constructor
            
    /// <summary>
            
    /// CustomersStruct Constructor
            
    /// </summary>
            public CustomersModel()
            {
            }
            
    /// <summary>
            
    /// CustomersStruct Constructor(Have Parameters)
            
    /// </summary>
            
    ///<param name="customerID">CustomerID</param>
            
    ///<param name="companyName">CompanyName</param>
            
    ///<param name="contactName">ContactName</param>
            
    ///<param name="contactTitle">ContactTitle</param>
            
    ///<param name="address">Address</param>
            
    ///<param name="city">City</param>
            
    ///<param name="region">Region</param>
            
    ///<param name="postalCode">PostalCode</param>
            
    ///<param name="country">Country</param>
            
    ///<param name="phone">Phone</param>
            
    ///<param name="fax">Fax</param>
            public CustomersModel(string customerID,string companyName,string contactName,string contactTitle,string address,string city,string region,string postalCode,string country,string phone,string fax)
            {
                
    this._customerID = customerID;
                
    this._companyName = companyName;
                
    this._contactName = contactName;
                
    this._contactTitle = contactTitle;
                
    this._address = address;
                
    this._city = city;
                
    this._region = region;
                
    this._postalCode = postalCode;
                
    this._country = country;
                
    this._phone = phone;
                
    this._fax = fax;
            }
            
    #endregion
           
            
    #region public Property
            
            
    ///<Summary>
            
    /// CustomerID
            
    ///</Summary>
            public string CustomerID
            {
                
    get { return _customerID; }
                
    set {  _customerID = value; }
            }
            
            
    ///<Summary>
            
    /// CompanyName
            
    ///</Summary>
            public string CompanyName
            {
                
    get { return _companyName; }
                
    set {  _companyName = value; }
            }
            
            
    ///<Summary>
            
    /// ContactName
            
    ///</Summary>
            public string ContactName
            {
                
    get { return _contactName; }
                
    set {  _contactName = value; }
            }
            
            
    ///<Summary>
            
    /// ContactTitle
            
    ///</Summary>
            public string ContactTitle
            {
                
    get { return _contactTitle; }
                
    set {  _contactTitle = value; }
            }
            
            
    ///<Summary>
            
    /// Address
            
    ///</Summary>
            public string Address
            {
                
    get { return _address; }
                
    set {  _address = value; }
            }
            
            
    ///<Summary>
            
    /// City
            
    ///</Summary>
            public string City
            {
                
    get { return _city; }
                
    set {  _city = value; }
            }
            
            
    ///<Summary>
            
    /// Region
            
    ///</Summary>
            public string Region
            {
                
    get { return _region; }
                
    set {  _region = value; }
            }
            
            
    ///<Summary>
            
    /// PostalCode
            
    ///</Summary>
            public string PostalCode
            {
                
    get { return _postalCode; }
                
    set {  _postalCode = value; }
            }
            
            
    ///<Summary>
            
    /// Country
            
    ///</Summary>
            public string Country
            {
                
    get { return _country; }
                
    set {  _country = value; }
            }
            
            
    ///<Summary>
            
    /// Phone
            
    ///</Summary>
            public string Phone
            {
                
    get { return _phone; }
                
    set {  _phone = value; }
            }
            
            
    ///<Summary>
            
    /// Fax
            
    ///</Summary>
            public string Fax
            {
                
    get { return _fax; }
                
    set {  _fax = value; }
            }
            
            
    #endregion        
       }
    }

    作者:chhuic

    出处:http://chhuic.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    家庭记账本安卓版开发:第六天
    django-自定义过滤器
    django-标签语法
    django-url的分发, 2)起别名, 3)根目录,4)rediect跳转函数:
    django-MTV基础篇
    django-第一天
    jquery---响应式方面应用
    css-基础知识
    HTML--第一章的基本知识
    003
  • 原文地址:https://www.cnblogs.com/chhuic/p/1639461.html
Copyright © 2020-2023  润新知