/*
'' ============================================================================================
'' 项目名称：风越项目范例ASPX普通三层结构
'' 项目版本：V1.0
'' 项目描述：风越项目范例，用于建立ASPX普通三层结构
'' 文件名称：DAL_CITY.cs
'' 文件描述：数据访问层 CITY （城市）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-12-25 20:23:50
'' 修订日期：2009-01-14 12:12:19
'' 版权信息：Copyright (C) 2008

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：风越代码生成器 [FireCode Creator]
'' 当前版本：通用版 V1.5
'' 官方网站：http://www.sino8848.com
'' ============================================================================================
*/

using System;
using System.Text;
using System.Data;


using System.Data.SqlClient;

using FypSystem.DBManager;

namespace Fyp_3TierCommon.DAL
{
    /// <summary>
    /// 数据访问类CITY。
    /// </summary>
    public class DAL_CITY
    {
        public DAL_CITY()
        {
        }

        #region  记录是否存在
        /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <param name="CITY_ID">CITY_ID</param>
        /// <returns></returns>
        public bool Exists(int? CITY_ID)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from [CITY] ");
            strSql.Append("where [CITY_ID] = @CITY_ID");
            

            
            

            
            
            SqlParameter[] arrParameters = new SqlParameter[1];
            
            arrParameters[0] = new SqlParameter("@CITY_ID", SqlDbType.Int, 10);
            arrParameters[0].Value = CITY_ID;
            
            

    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                

                
                return Convert.ToBoolean(dbManager.ExecScalar(strSql.ToString(), arrParameters));
                
            }
        }
        #endregion

        #region 增加数据
        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="model">CITY实体对象</param>
        public void Add(Fyp_3TierCommon.Model.Model_CITY model)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [CITY] (");
            strSql.Append("PROVINCE_ID, CITY_CN, CITY_EN, CITY_AREA_CODE");
            strSql.Append(") values (");
            strSql.Append("@PROVINCE_ID, @CITY_CN, @CITY_EN, @CITY_AREA_CODE");
            strSql.Append(")");
            

            
            

            
            
            SqlParameter[] arrParameters = new SqlParameter[4];
            
            arrParameters[0] = new SqlParameter("@PROVINCE_ID", SqlDbType.Int, 10);
            arrParameters[0].Value = model.PROVINCE_ID;
            
            arrParameters[1] = new SqlParameter("@CITY_CN", SqlDbType.NVarChar, 50);
            arrParameters[1].Value = model.CITY_CN;
            
            arrParameters[2] = new SqlParameter("@CITY_EN", SqlDbType.NVarChar, 100);
            arrParameters[2].Value = model.CITY_EN;
            
            arrParameters[3] = new SqlParameter("@CITY_AREA_CODE", SqlDbType.Int, 10);
            arrParameters[3].Value = model.CITY_AREA_CODE;
            
            

    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                

                
                dbManager.ExecNonQuery(strSql.ToString(), arrParameters);
                
            }
        }
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="model">CITY实体对象</param>
        public void Update(Fyp_3TierCommon.Model.Model_CITY model)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [CITY] set ");
            
            strSql.Append("PROVINCE_ID=@PROVINCE_ID");
            strSql.Append("CITY_CN=@CITY_CN");
            strSql.Append("CITY_EN=@CITY_EN");
            strSql.Append("CITY_AREA_CODE=@CITY_AREA_CODE");
            strSql.Append(" where [CITY_ID] = @CITY_ID");
            

            
            

            
            
            SqlParameter[] arrParameters = new SqlParameter[5];
            
            arrParameters[0] = new SqlParameter("@CITY_ID", SqlDbType.Int, 10);
            arrParameters[0].Value = model.CITY_ID;
            
            arrParameters[1] = new SqlParameter("@PROVINCE_ID", SqlDbType.Int, 10);
            arrParameters[1].Value = model.PROVINCE_ID;
            
            arrParameters[2] = new SqlParameter("@CITY_CN", SqlDbType.NVarChar, 50);
            arrParameters[2].Value = model.CITY_CN;
            
            arrParameters[3] = new SqlParameter("@CITY_EN", SqlDbType.NVarChar, 100);
            arrParameters[3].Value = model.CITY_EN;
            
            arrParameters[4] = new SqlParameter("@CITY_AREA_CODE", SqlDbType.Int, 10);
            arrParameters[4].Value = model.CITY_AREA_CODE;
            
            

    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                

                
                dbManager.ExecNonQuery(strSql.ToString(), arrParameters);
                
            }
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="CITY_ID">CITY_ID</param>
        public void Delete(int? CITY_ID)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CITY] ");
            strSql.Append(" where [CITY_ID] = @CITY_ID");
            

            
            

            
            
            SqlParameter[] arrParameters = new SqlParameter[1];
            
            arrParameters[0] = new SqlParameter("@CITY_ID", SqlDbType.Int, 10);
            arrParameters[0].Value = CITY_ID;
            
            

    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                

                
                dbManager.ExecNonQuery(strSql.ToString(), arrParameters);
                
            }
        }
        #endregion

        #region 获取对象实体
        /// <summary>
        /// 获取对象实体
        /// </summary>
        /// <param name="CITY_ID">CITY_ID</param>
        /// <returns></returns>
        public Fyp_3TierCommon.Model.Model_CITY GetModel(int? CITY_ID)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CITY_ID, PROVINCE_ID, CITY_CN, CITY_EN, CITY_AREA_CODE from [CITY]");
            strSql.Append(" where [CITY_ID] = @CITY_ID");
            

            
            

            
            
            SqlParameter[] arrParameters = new SqlParameter[1];
            
            arrParameters[0] = new SqlParameter("@CITY_ID", SqlDbType.Int, 10);
            arrParameters[0].Value = CITY_ID;
            
            

            Fyp_3TierCommon.Model.Model_CITY model = new Fyp_3TierCommon.Model.Model_CITY();

            DataSet ds = new DataSet();;
    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                

                
                ds = dbManager.GetDataSet(strSql.ToString(), arrParameters);
                
            }

            if(ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];

            
                

                 
                if (dr["CITY_ID"].ToString().Length > 0)
                    model.CITY_ID = int.Parse(dr["CITY_ID"].ToString());
                

                
            
                

                 
                if (dr["PROVINCE_ID"].ToString().Length > 0)
                    model.PROVINCE_ID = int.Parse(dr["PROVINCE_ID"].ToString());
                

                
            
                 
                model.CITY_CN = dr["CITY_CN"].ToString();
                

                

                
            
                 
                model.CITY_EN = dr["CITY_EN"].ToString();
                

                

                
            
                

                 
                if (dr["CITY_AREA_CODE"].ToString().Length > 0)
                    model.CITY_AREA_CODE = int.Parse(dr["CITY_AREA_CODE"].ToString());
                

                
            

                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 获取数据列表
        /// <summary>
        /// 获取数据列表 DataSet
        /// </summary>
        /// <param name="strWhere">指定查询条件的SQL语句，不包含where</param>
        /// <returns></returns>
        public DataSet GetDataSet(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CITY_ID, PROVINCE_ID, CITY_CN, CITY_EN, CITY_AREA_CODE from [CITY]");
            if(strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                return dbManager.GetDataSet(strSql.ToString());
            }
        }

        /// <summary>
        /// 获取数据列表 DataTable
        /// </summary>
        /// <param name="strWhere">指定查询条件的SQL语句，不包含where</param>
        /// <returns></returns>
        public DataTable GetDataTable(string strWhere)
        {
            return GetDataTable(strWhere, "");
        }

        /// <summary>
        /// 获取数据列表 DataTable
        /// </summary>
        /// <param name="strWhere">指定查询条件的SQL语句，不包含where</param>
        /// <param name="strOrder">排序字段，如id desc，不包含order by</param>
        /// <returns></returns>
        public DataTable GetDataTable(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CITY_ID, PROVINCE_ID, CITY_CN, CITY_EN, CITY_AREA_CODE from [CITY]");
            if(strWhere.Trim().Length > 0)
                strSql.Append(" where " + strWhere);

            if (strOrder.Trim().Length > 0)
                strSql.Append(" order by " + strOrder);

    
    
            using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
    
            {
                return dbManager.GetDataTable(strSql.ToString());
            }
        }
        #endregion 
    }
}