zoukankan      html  css  js  c++  java
  • 转贴:asp.netN层代码示例

    一、数据库Test,
    表:

    create table Customers
    (
     CustId 
    int IDENTITY(1,1) primary key,
     CustName varchar(
    20) not null,
     Address varchar(
    50),
     Linkman varchar(
    20)
    )
    //insert into Cusomers values('ggg','xuzhou','zhangsan');


     
    二、配置文件web.config

    <?xml version="1.0"?>
    <configuration>
     
    <appSettings/>
     
    <connectionStrings>
      
    <add name="TestConnectionString" connectionString="Data Source=GONGCHL;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
     
    </connectionStrings>
     
    <system.web>
      
    <compilation debug="true"/>
      
    <authentication mode="Windows"/>
     
    </system.web>
    </configuration>
     

    三、业务实体

     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4 
     5namespace com.Model
     6{
     7    /// <summary>
     8    /// 业务实体CustomerInfo
     9    /// </summary>

    10    [Serializable]
    11    public class CustomerInfo
    12    {
    13 
    14        /// <summary>
    15        /// 默认的构造函数
    16        /// </summary> 

    17        public CustomerInfo()  {}
    18 
    19        /// <summary>
    20        /// 有参数的构造函数
    21        /// </summary> 
    22        /// <param name="custId">客户号</param>
    23        /// <param name="custName">客户名称</param>
    24        /// <param name="address">客户地址</param>
    25        /// <param name="linkman">联系人</param>

    26        public CustomerInfo(int custId, string custName, string address, string linkman)
    27       {
    28 
    29            this.custId = custId;
    30            this.custName = custName;
    31            this.address = address;
    32            this.linkman = linkman;
    33        }

    34        private int custId;
    35        public int CustId
    36        {
    37            get return custId; }
    38            set { custId = value; }
    39        }

    40 
    41        private string custName;
    42        public string CustName
    43        {
    44            get return custName; }
    45            set { custName = value; }
    46        }

    47 
    48        private string address;
    49        public string Address
    50        {
    51            get return address; }
    52            set { address = value; }
    53        }

    54 
    55        private string linkman;
    56        public string Linkman
    57        {
    58            get return linkman; }
    59            set { linkman = value; }
    60        }

    61    }

    62}

    63

    四、数据访问层
    类:SqlHelper

      1//===============================================================================
      2// .NET数据访问通用程序,来自Microsoft公司
      3// 更多信息参见
      4// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
      5//===============================================================================
      6 
      7using System;
      8using System.Configuration;
      9using System.Data;
     10using System.Data.SqlClient;
     11using System.Collections;
     12 
     13namespace com.DataAccess
     14{
     15 
     16    /// <summary>
     17    /// SqlHelper类提供很高的数据访问性能, 
     18    /// 使用SqlClient类的通用定义.
     19    /// </summary>

     20    public abstract class SqlHelper
     21    {
     22 
     23        //定义数据库连接串
     24        public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
     25        //public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
     26        //public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
     27        //public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
     28 
     29        // 存贮Cache缓存的Hashtable集合
     30        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
     31 
     32        /// <summary>
     33        /// 使用连接字符串,执行一个SqlCommand命令(没有记录返回)
     34        /// 使用提供的参数集.
     35        /// </summary>
     36        /// <remarks>
     37        /// 示例:  
     38        ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
     39        /// </remarks>
     40        /// <param name="connectionString">一个有效的SqlConnection连接串</param>
     41        /// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
     42        /// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
     43        /// <param name="commandParameters">执行命令的参数集</param>
     44        /// <returns>受此命令影响的行数</returns>

     45        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
     46        {
     47 
     48            SqlCommand cmd = new SqlCommand();
     49 
     50            using (SqlConnection conn = new SqlConnection(connectionString))
     51            {
     52                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
     53                int val = cmd.ExecuteNonQuery();
     54                cmd.Parameters.Clear();
     55                return val;
     56            }

     57        }

     58 
     59        /// <summary>
     60        /// 在一个存在的连接上执行数据库的命令操作
     61        /// 使用提供的参数集.
     62        /// </summary>
     63        /// <remarks>
     64        /// e.g.:  
     65        ///  int result = ExecuteNonQuery(connection, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
     66        /// </remarks>
     67        /// <param name="conn">一个存在的数据库连接对象</param>
     68        /// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
     69        /// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
     70        /// <param name="commandParameters">执行命令的参数集</param>
     71        /// <returns>受此命令影响的行数</returns>

     72        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
     73        {
     74 
     75            SqlCommand cmd = new SqlCommand();
     76 
     77            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
     78            int val = cmd.ExecuteNonQuery();
     79            cmd.Parameters.Clear();
     80            return val;
     81        }

     82 
     83        /// <summary>
     84        /// 在一个事务的连接上执行数据库的命令操作
     85        /// 使用提供的参数集.
     86        /// </summary>
     87        /// <remarks>
     88        /// e.g.:  
     89        ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
     90        /// </remarks>
     91        /// <param name="trans">一个存在的事务</param>
     92        /// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
     93        /// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
     94        /// <param name="commandParameters">执行命令的参数集</param>
     95        /// <returns>受此命令影响的行数</returns>

     96        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
     97        {
     98            SqlCommand cmd = new SqlCommand();
     99            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
    100            int val = cmd.ExecuteNonQuery();
    101            cmd.Parameters.Clear();
    102            return val;
    103        }

    104 
    105        /// <summary>
    106        /// 在一个连接串上执行一个命令,返回一个SqlDataReader对象
    107        /// 使用提供的参数.
    108        /// </summary>
    109        /// <remarks>
    110        /// e.g.:  
    111        ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    112        /// </remarks>
    113        /// <param name="connectionString">一个有效的SqlConnection连接串</param>
    114        /// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
    115        /// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
    116        /// <param name="commandParameters">执行命令的参数集</param>
    117        /// <returns>一个结果集对象SqlDataReader</returns>

    118        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
    119        {
    120            SqlCommand cmd = new SqlCommand();
    121            SqlConnection conn = new SqlConnection(connectionString);
    122 
    123            // 如果不存在要查询的对象,则发生异常
    124            // 连接要关闭
    125            // CommandBehavior.CloseConnection在异常时不发生作用
    126            try
    127            {
    128                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
    129                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    130                cmd.Parameters.Clear();
    131                return rdr;
    132            }

    133            catch
    134            {
    135                conn.Close();
    136                throw;
    137            }

    138        }

    139 
    140        /// <summary>
    141        /// 在一个连接串上执行一个命令,返回表中第一行,第一列的值
    142        /// 使用提供的参数.
    143        /// </summary>
    144        /// <remarks>
    145        /// e.g.:  
    146        ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    147        /// </remarks>
    148        /// <param name="connectionString">一个有效的SqlConnection连接串</param>
    149        /// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
    150        /// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
    151        /// <param name="commandParameters">执行命令的参数集</param>        /// <returns>返回的对象,在使用时记得类型转换</returns>

    152        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
    153        {
    154            SqlCommand cmd = new SqlCommand();
    155 
    156            using (SqlConnection connection = new SqlConnection(connectionString))
    157            {
    158                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
    159                object val = cmd.ExecuteScalar();
    160                cmd.Parameters.Clear();
    161                return val;
    162            }

    163        }

    164 
    165        /// <summary>
    166        /// 在一个连接上执行一个命令,返回表中第一行,第一列的值
    167        /// 使用提供的参数.
    168        /// </summary>
    169        /// <remarks>
    170        /// e.g.:  
    171        ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    172        /// </remarks>
    173        /// <param name="connectionString">一个有效的SqlConnection连接</param>
    174        /// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
    175        /// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
    176        /// <param name="commandParameters">执行命令的参数集</param>        /// <returns>返回的对象,在使用时记得类型转换</returns>

    177        public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
    178        {
    179 
    180            SqlCommand cmd = new SqlCommand();
    181 
    182            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
    183            object val = cmd.ExecuteScalar();
    184            cmd.Parameters.Clear();
    185            return val;
    186        }

    187 
    188        /// <summary>
    189        /// 在缓存中添加参数数组
    190        /// </summary>
    191        /// <param name="cacheKey">参数的Key</param>
    192        /// <param name="cmdParms">参数数组</param>

    193        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
    194        {
    195            parmCache[cacheKey] = commandParameters;
    196        }

    197 
    198        /// <summary>
    199        /// 提取缓存的参数数组
    200        /// </summary>
    201        /// <param name="cacheKey">查找缓存的key</param>
    202        /// <returns>返回被缓存的参数数组</returns>

    203        public static SqlParameter[] GetCachedParameters(string cacheKey)
    204        {
    205            SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
    206 
    207            if (cachedParms == null)
    208                return null;
    209 
    210            SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
    211 
    212            for (int i = 0, j = cachedParms.Length; i < j; i++)
    213                clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
    214 
    215            return clonedParms;
    216        }

    217 
    218        /// <summary>
    219        /// 提供一个SqlCommand对象的设置
    220        /// </summary>
    221        /// <param name="cmd">SqlCommand对象</param>
    222        /// <param name="conn">SqlConnection 对象</param>
    223        /// <param name="trans">SqlTransaction 对象</param>
    224        /// <param name="cmdType">CommandType 如存贮过程,T-SQL</param>
    225        /// <param name="cmdText">存贮过程名或查询串</param>
    226        /// <param name="cmdParms">命令中用到的参数集</param>

    227        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
    228        {
    229 
    230            if (conn.State != ConnectionState.Open)
    231                conn.Open();
    232 
    233            cmd.Connection = conn;
    234            cmd.CommandText = cmdText;
    235 
    236            if (trans != null)
    237                cmd.Transaction = trans;
    238 
    239            cmd.CommandType = cmdType;
    240 
    241            if (cmdParms != null)
    242            {
    243                foreach (SqlParameter parm in cmdParms)
    244                    cmd.Parameters.Add(parm);
    245            }

    246        }

    247    }

    248}

    249 
    250

     
    类:Customer

      1using System;
      2using System.Data.SqlClient;
      3using System.Data;
      4using System.Text;
      5using System.Collections.Generic;
      6using com.Model;
      7 
      8namespace com.DataAccess
      9{
     10    /// <summary>
     11    /// 对客户表的所有数据访问操作
     12    /// </summary>

     13    public class Customer
     14    {
     15 
     16        //静态常量,参数名,T-SQL串
     17        private const string SQL_SELECT_CUSTOMER_BY_ID = 
     18            "SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS WHERE CustID = @CustId";
     19        private const string SQL_SELECT_CUSTOMER_BY_NAME =
     20            "SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS WHERE CustName = @CustName";
     21        private const string SQL_SELECT_CUSTOMER_BY_ALL = 
     22            "SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS";
     23        private const string SQL_UPDATE_CUSTOMER_BY_ID = 
     24            "UPDATE CUSTOMERS SET CustName=@CustName, Address=@Address, Linkman = @Linkman WHERE CustId=@CustId ";
     25        private const string SQL_DELETE_CUSTOMER_BY_ID = 
     26            "DELETE CUSTOMERS WHERE CustId=@CustId ";
     27        private const string SQL_INSERT_CUSTOMER =
     28            "Declare @ID int;INSERT INTO CUSTOMERS VALUES(@CustName, @Address, @Linkman);SELECT @ID = @@IDENTITY; SELECT @ID";
     29        
     30        private const string PARM_CUSTOMERID = "@CustId";
     31        private const string PARM_CUSTOMERNAME = "@CustName";
     32        private const string PARM_ADDRESS = "@Address";
     33        private const string PARM_LINKMAN = "@Linkman";
     34 
     35        /// <summary>
     36        /// 按客户ID查询
     37        /// </summary>
     38        /// <param name="custId">客户号</param>  
     39        /// <returns>客户对象</returns>

     40        public CustomerInfo GetCustomerById(int custId)
     41        {
     42            CustomerInfo customerInfo=null;
     43            SqlParameter parm = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
     44            parm.Value = custId;
     45 
     46            //按客户号参数执行查询得到一个客户信息
     47            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_ID, parm))
     48            {
     49                if (rdr.Read())
     50                    customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
     51            }

     52            return customerInfo;
     53        }

     54 
     55        /// <summary>
     56        /// 按客户名称查询
     57        /// </summary>
     58        /// <param name="custName">客户名称</param>  
     59        /// <returns>客户对象</returns>

     60        public CustomerInfo GetCustomerByName(string custName)
     61        {
     62            CustomerInfo customerInfo = null;
     63            SqlParameter parm = new SqlParameter(PARM_CUSTOMERNAME, SqlDbType.VarChar,20);
     64            parm.Value = custName;
     65 
     66            //按客户号参数执行查询得到一个客户信息
     67            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_NAME, parm))
     68            {
     69                if (rdr.Read())
     70                    customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
     71            }

     72            return customerInfo;
     73        }

     74 
     75        /// <summary>
     76        /// 查询所有客户信息
     77        /// 结果为IList
     78        /// </summary>
     79        /// <returns>一个客户集合</returns>

     80        public IList<CustomerInfo> GetCusomersByAll()
     81        {
     82 
     83            IList<CustomerInfo> customers = new List<CustomerInfo>();
     84 
     85            //Finally execute the query
     86            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_ALL, null))
     87            {
     88                while (rdr.Read())
     89                {
     90                    CustomerInfo customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
     91                    customers.Add(customerInfo);
     92                }

     93            }

     94 
     95            return customers;
     96        }

     97        /// <summary>
     98        /// 插入一个客户信息
     99        /// </summary>
    100        /// <param name="customer">客户对象CustomerInfo</param>  
    101        /// <returns>bool类型,true or false</returns>

    102        public bool InsertCustomer(CustomerInfo customerInfo)
    103        {
    104            SqlParameter[] paras = new SqlParameter[3];
    105            paras[0]=new SqlParameter(PARM_CUSTOMERNAME,SqlDbType.VarChar,20);
    106            paras[0].Value=customerInfo.CustName;
    107            paras[1]=new SqlParameter(PARM_ADDRESS,SqlDbType.VarChar,50);
    108            paras[1].Value=customerInfo.Address;
    109            paras[2]=new SqlParameter(PARM_LINKMAN,SqlDbType.VarChar,20);
    110            paras[2].Value=customerInfo.Linkman;
    111 
    112            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_INSERT_CUSTOMER, paras))
    113            {
    114                if (rdr.Read())
    115                    customerInfo.CustId = rdr.GetInt32(0);
    116                else
    117                    return false;
    118            }

    119            return true;
    120        }

    121 
    122        /// <summary>
    123        /// 修改一个客户信息
    124        /// </summary>
    125        /// <param name="customer">客户对象CustomerInfo</param>  
    126        /// <returns>bool类型,true or false</returns>

    127        public bool UpdateCustomerByID(CustomerInfo customerInfo)
    128        {
    129            SqlParameter[] paras = new SqlParameter[4];
    130            paras[0= new SqlParameter(PARM_CUSTOMERNAME, SqlDbType.VarChar, 20);
    131            paras[0].Value = customerInfo.CustName;
    132            paras[1= new SqlParameter(PARM_ADDRESS, SqlDbType.VarChar, 50);
    133            paras[1].Value = customerInfo.Address;
    134            paras[2= new SqlParameter(PARM_LINKMAN, SqlDbType.VarChar, 20);
    135            paras[2].Value = customerInfo.Linkman;
    136            paras[3= new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
    137            paras[3].Value = customerInfo.CustId;
    138 
    139            int row = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_UPDATE_CUSTOMER_BY_ID, paras);
    140            if (row == 0)
    141                return false;
    142            return true;
    143        }

    144 
    145        /// <summary>
    146        /// 按ID删除一个客户信息
    147        /// </summary>
    148        /// <param name="custId">客户号</param>  
    149        /// <returns>bool类型,true or false</returns>

    150        public bool DeleteCustomerByID(int custId)
    151        {
    152            SqlParameter para = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
    153            para.Value = custId;
    154 
    155            int row = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_DELETE_CUSTOMER_BY_ID, para);
    156            if (row == 0)
    157                return false;
    158            return true;
    159        }

    160 
    161    }

    162}

     
    五、业务逻辑层

     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4using com.DataAccess;
     5using com.Model;
     6using com.BusinessRule;
     7 
     8namespace com.BusinessLogic
     9{
    10    public class CustomerLogic
    11    {
    12        /// <summary>
    13        /// 插入一个客户信息
    14        /// </summary>
    15        /// <param name="custId">客户号</param>  
    16        /// <param name="custName">客户名称</param>  
    17        /// <param name="address">客户地址</param>  
    18        /// <param name="linkman">联系人</param>  
    19        /// <returns>bool类型,true or false</returns>

    20        public bool InsertCustomer(int custId,string custName, string address, string linkman)
    21        {
    22            if (CustomerRule.IsExistCustomerName(custName))
    23                return false;
    24            Customer customer = new Customer();
    25            CustomerInfo customerInfo = new CustomerInfo(custId,custName,address,linkman);
    26            return customer.InsertCustomer(customerInfo);
    27        }

    28 
    29        /// <summary>
    30        /// 插入一个客户信息
    31        /// </summary>
    32        /// <param name="custName">客户名称</param>  
    33        /// <param name="address">客户地址</param>  
    34        /// <param name="linkman">联系人</param>  
    35        /// <returns>bool类型,true or false</returns>

    36        public bool InsertCustomer(string custName, string address, string linkman)
    37        {
    38            if (CustomerRule.IsExistCustomerName(custName))
    39                return false;
    40            Customer customer = new Customer();
    41            CustomerInfo customerInfo = new CustomerInfo(0, custName, address, linkman);
    42            return customer.InsertCustomer(customerInfo);
    43        }

    44 
    45        /// <summary>
    46        /// 修改一个客户信息
    47        /// </summary>
    48        /// <param name="custId">客户号</param>  
    49        /// <param name="custName">客户名称</param>  
    50        /// <param name="address">客户地址</param>  
    51        /// <param name="linkman">联系人</param>  
    52        /// <returns>bool类型,true or false</returns>

    53        public bool UpdateCustomer(int custId,string custName, string address, string linkman)
    54        {
    55            Customer customer = new Customer();
    56            CustomerInfo customerInfo = new CustomerInfo(custId, custName, address, linkman);
    57            return customer.UpdateCustomerByID(customerInfo);
    58        }

    59 
    60        /// <summary>
    61        /// 按ID删除一个客户信息
    62        /// </summary>
    63        /// <param name="custId">客户号</param>  
    64        /// <returns>bool类型,true or false</returns>

    65        public bool DeleteCustomerByID(int custId)
    66        {
    67            Customer customer = new Customer();
    68            return customer.DeleteCustomerByID(custId);
    69        }

    70 
    71 
    72        /// <summary>
    73        /// 查询所有客户信息
    74        /// 结果为IList
    75        /// </summary>
    76        /// <returns>一个客户集合</returns>

    77        public IList<CustomerInfo> GetCustomersByAll()
    78        {
    79            Customer customer = new Customer();
    80            return customer.GetCusomersByAll();
    81        }

    82    }

    83}

    84

    六、业务规则层

     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4using com.DataAccess;
     5using com.Model;
     6 
     7namespace com.BusinessRule
     8{
     9    /// <summary>
    10    /// 检查客户信息的合法性
    11    /// </summary>

    12    public class CustomerRule
    13    {
    14        /// <summary>
    15        /// 检查客户的名称是否已经存在
    16        /// </summary>
    17        /// <remarks>
    18        /// e.g.:  
    19        ///  bool exist =CustomerRule.IsExistCustomerName(custName);
    20        /// </remarks>
    21        /// <param name="custName">客户名称</param>
    22        /// <returns>客户存在与否</returns>

    23 
    24        public static bool IsExistCustomerName(string custName)
    25        {
    26            Customer cust = new Customer();
    27            CustomerInfo custInfo = cust.GetCustomerByName(custName);
    28            if (custInfo == null)
    29                    return false;
    30            else
    31                    return true;
    32        }

    33    }

    34}

    35

    七、业务外观层

     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4using System.Web.UI.WebControls;
     5using com.BusinessLogic;
     6using com.Model;
     7 
     8namespace com.BusinessFacade
     9{
    10    /// <summary>
    11    /// 为界面中Table处理数据
    12    /// </summary>

    13    public class CustomerTable
    14    {
    15        public static void SetTableData(Table table)
    16        {
    17            IList<CustomerInfo> list = new CustomerLogic().GetCustomersByAll();
    18 
    19            AddRowHead(table);
    20            foreach (CustomerInfo cust in list)
    21            {
    22                AddRow(table, cust);
    23            }

    24        }

    25 
    26        private static void AddRowHead(Table table)
    27        {
    28            TableCell cell = new TableCell();
    29            cell.Text = "Head";
    30            TableRow row = new TableRow();
    31            row.Cells.Add(cell);
    32            table.Rows.Add(row);
    33 
    34        }

    35        private static void AddRow(Table table, CustomerInfo cust)
    36        {
    37            TableRow row = new TableRow();
    38            TableCell cell1 = new TableCell();
    39            cell1.Text = cust.CustId.ToString();
    40            TableCell cell2 = new TableCell();
    41            cell2.Text = cust.CustName;
    42            TableCell cell3 = new TableCell();
    43            cell3.Text = cust.Address;
    44            TableCell cell4 = new TableCell();
    45            cell4.Text = cust.Linkman;
    46            row.Cells.Add(cell1);
    47            row.Cells.Add(cell2);
    48            row.Cells.Add(cell3);
    49            row.Cells.Add(cell4);
    50 
    51            table.Rows.Add(row);
    52        }

    53    }

    54}

    55

     
    八、界面层

     1<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
     2 
     3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 
     5<html xmlns="http://www.w3.org/1999/xhtml" >
     6<head runat="server">
     7    <title>无标题页</title>
     8</head>
     9<body>
    10    <form id="form1" runat="server">
    11    <div>
    12        &nbsp;</div>
    13        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    14            InsertMethod="InsertCustomer"  SelectMethod="GetCustomersByAll" TypeName="com.BusinessLogic.CustomerLogic" DeleteMethod="DeleteCustomerByID" UpdateMethod="UpdateCustomer">
    15            <DeleteParameters>
    16                <asp:ControlParameter ControlID="FormView1" PropertyName="SelectedValue" Name="custId"  Type="Int32" />
    17            </DeleteParameters>
    18            <UpdateParameters>
    19                <asp:Parameter Name="custId" Type="Int32" />
    20                <asp:Parameter Name="custName" Type="String" />
    21                <asp:Parameter Name="address" Type="String" />
    22                <asp:Parameter Name="linkman" Type="String" />
    23            </UpdateParameters>
    24            <InsertParameters>
    25                <asp:Parameter Name="custName" Type="String" />
    26                <asp:Parameter Name="address" Type="String" />
    27                <asp:Parameter Name="linkman" Type="String" />
    28            </InsertParameters>
    29        </asp:ObjectDataSource>
    30        <asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" AllowPaging="True" DataKeyNames="custId">
    31            <EditItemTemplate>
    32                CustName:
    33                <asp:TextBox ID="CustNameTextBox" runat="server" Text='<%# Bind("CustName") %>'></asp:TextBox><br />
    34                Address:
    35                <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox><br />
    36                Linkman:
    37                <asp:TextBox ID="LinkmanTextBox" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox><br />
    38                CustId:
    39                <asp:TextBox ID="CustIdTextBox" runat="server" BorderStyle="None" Enabled="False"
    40                    Text='<%# Bind("CustId") %>'></asp:TextBox><br />
    41                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
    42                    Text="更新"></asp:LinkButton>
    43                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
    44                    Text="取消"></asp:LinkButton>
    45            </EditItemTemplate>
    46            <InsertItemTemplate>
    47                CustName:
    48                <asp:TextBox ID="CustNameTextBox" runat="server" Text='<%# Bind("CustName") %>'></asp:TextBox><br />
    49                Address:
    50                <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox><br />
    51                Linkman:
    52                <asp:TextBox ID="LinkmanTextBox" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox><br />
    53                CustId:
    54                <asp:TextBox ID="CustIdTextBox" runat="server"  Text='0' Enabled="False"></asp:TextBox><br />
    55                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
    56                    Text="插入"></asp:LinkButton>
    57                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
    58                    Text="取消"></asp:LinkButton>
    59            </InsertItemTemplate>
    60            <ItemTemplate>
    61                CustName:
    62                <asp:Label ID="CustNameLabel" runat="server" Text='<%# Bind("CustName") %>'></asp:Label><br />
    63                Address:
    64                <asp:Label ID="AddressLabel" runat="server" Text='<%# Bind("Address") %>'></asp:Label><br />
    65                Linkman:
    66                <asp:Label ID="LinkmanLabel" runat="server" Text='<%# Bind("Linkman") %>'></asp:Label><br />
    67                CustId:
    68                <asp:Label ID="CustIdLabel" runat="server" Enabled="False" Text='<%# Bind("CustId") %>'></asp:Label><br />
    69                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
    70                    Text="编辑"></asp:LinkButton>
    71                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
    72                    Text="删除" ></asp:LinkButton>
    73                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
    74                    Text="新建"></asp:LinkButton>
    75            </ItemTemplate>
    76        </asp:FormView>
    77        <asp:Table ID="Table1" runat="server">
    78        </asp:Table>
    79        &nbsp;
    80    </form>
    81</body>
    82</html>
    83
  • 相关阅读:
    [转]ARM QT实现多点触摸
    Accessors are only available when targeting ECMAScript 5 and higher 错误提示
    设置全局变量
    ERROR Error: Cannot find module ‘vueloaderv16/package.json‘
    JVM系列三:JVM参数设置、分析
    HDU 1407 测试你是否和LTC水平一样高
    HDU 1276 士兵队列训练问题
    python类的学习笔记1
    manjaro更新到了20版本fcitx5出问题的解决方法
    图片网站
  • 原文地址:https://www.cnblogs.com/skyfei/p/485206.html
Copyright © 2011-2022 走看看