考:清华大学出版社<asp.net web应用程序设计及开发(c#版)>三层结构


1.创建数据层

   创建对客户进行操作的存储过程.存储过程是数据层的一部分,可以在业务层中访问它,以实现表示层的某个地方要执行的功能.这里不再说明把数据访问逻辑放在单独一层的优点.

   在Store数据库中创建存储过程GetCustomerByEmail,该存储过程把登录页面中输入的EMAIL作为参数,查找EMAIL地址为该参数值的客户,然后以输出参数的形式返回该客户的ID,名字,密码 ,地址,国家,电话和传真.代码如下:

[csharp]  view plain copy
  1. create procedure GetCustomerByEmail  
  2.   
  3. (  
  4.   
  5.       @Email nvarchar(50) = null,  
  6.   
  7.       @CustomerID int OUTPUT,  
  8.   
  9.       @Name nvarchar(50) OUTPUT,  
  10.   
  11.       @password nvarchar(50) OUTPUT,  
  12.   
  13.       @Address nvarchar(250) OUTPUT,  
  14.   
  15.       @Country nvarchar(250) OUTPUT,  
  16.   
  17.       @PhoneNumber nvarchar(250) OUTPUT,  
  18.   
  19.       @Fax nvarchar(250) OUTPUT  
  20.   
  21.    )  
  22.   
  23. AS  
  24.   
  25.       SET NOCOUNT ON  
  26.   
  27. SELECT   
  28.   
  29.       @CustomerID = c.CutomerID,  
  30.   
  31.       @Name = c.FullName,  
  32.   
  33.       @Password = c.Password,  
  34.   
  35.       @Address = a.Address,  
  36.   
  37.       @Country = a.Country,  
  38.   
  39.       @PhoneNumber = a.PhoneNumber,  
  40.   
  41.       @Fax = a.Fax  
  42.   
  43. FROM Customers c,Addresses a  
  44.   
  45.       WHERE c.EmailAddree = @Email  
  46.   
  47.       AND a.CustomerId = c.CustomerId  
  48.   
  49.    IF @@Rowcount < 1  
  50.   
  51. SELECT   
  52.   
  53.       @CustomerID = 0  
  54.   
  55. GO  
  56.   
  57. (  
  58.   
  59.       @Email nvarchar(50) = null,  
  60.   
  61.       @CustomerID int OUTPUT,  
  62.   
  63.       @Name nvarchar(50) OUTPUT,  
  64.   
  65.       @password nvarchar(50) OUTPUT,  
  66.   
  67.       @Address nvarchar(250) OUTPUT,  
  68.   
  69.       @Country nvarchar(250) OUTPUT,  
  70.   
  71.       @PhoneNumber nvarchar(250) OUTPUT,  
  72.   
  73.       @Fax nvarchar(250) OUTPUT  
  74.   
  75.    )  
  76.   
  77. AS  
  78.   
  79.       SET NOCOUNT ON  
  80.   
  81. SELECT   
  82.   
  83.       @CustomerID = c.CutomerID,  
  84.   
  85.       @Name = c.FullName,  
  86.   
  87.       @Password = c.Password,  
  88.   
  89.       @Address = a.Address,  
  90.   
  91.       @Country = a.Country,  
  92.   
  93.       @PhoneNumber = a.PhoneNumber,  
  94.   
  95.       @Fax = a.Fax  
  96.   
  97. FROM Customers c,Addresses a  
  98.   
  99.       WHERE c.EmailAddree = @Email  
  100.   
  101.       AND a.CustomerId = c.CustomerId  
  102.   
  103.    IF @@Rowcount < 1  
  104.   
  105. SELECT   
  106.   
  107.       @CustomerID = 0  
  108.   
  109. GO  

2.创建业务层

业务层是存取数据的类,该类与数据层和表示层(用户界面)交户作用,在需要时从表示层获得数据,并将数据存入数据库,也可以从数据层获得数据,并将数据发送给表示层(用户界面).

(1)在"解决方案资源管理器"中,选择解决方案下的GoShop项目,右击,再将鼠标指向"添加",然后选择"添加类"命令.

   (2)在"类别"窗口中选择"本地项目",然后在"模板"窗口中选择"代码文件".在"名称"文本框中,输入CustomerDB.

   (3)单击"打开",代码如下:

[csharp]  view plain copy
  1. using System;  
  2.   
  3. using System.Data;  
  4.   
  5. using System.Configuration;  
  6.   
  7. using System.Web;  
  8.   
  9. using System.Web.Security;  
  10.   
  11. using System.Web.UI;  
  12.   
  13. using System.Web.UI.WebControls;  
  14.   
  15. using System.Web.UI.WebControls.WebParts;  
  16.   
  17. using System.Web.UI.HtmlControls;  
  18.   
  19. using System.Data.SqlClient;  
  20.   
  21.   
  22.   
  23. namespace Goshop{  
  24.   
  25.   
  26.   
  27. /// <summary>  
  28.   
  29. /// Class1 的摘要说明  
  30.   
  31. /// </summary>  
  32.   
  33. public class Customer  
  34.   
  35. {  
  36.   
  37.   
  38.   
  39.    public string Name;  
  40.   
  41.         public string Email;  
  42.   
  43.         public string Password;  
  44.   
  45.         public string Country;  
  46.   
  47.         public string Addresss;  
  48.   
  49.         public string PhoneNumber;  
  50.   
  51.         public string Fax;  
  52.   
  53.         public int CustomerID;  
  54.   
  55.   
  56.   
  57. }  
  58.   
  59.     public class CustomerDB  
  60.   
  61.     {  
  62.   
  63.           private string ConnectionString  
  64.   
  65.           {  
  66.   
  67.               get  
  68.   
  69.               {  
  70.   
  71.                   string strConn;  
  72.   
  73.                   strConn = "Data Source =(local);";  
  74.   
  75.                   strConn +="Initial Catalog = Store;";  
  76.   
  77.                  return strConn ;  
  78.   
  79.               }  
  80.   
  81.           }  
  82.   
  83.         public Customer GetCustomerByEmail(string Email)  
  84.   
  85.     {  
  86.   
  87.         SqlConnection myconnection = new SqlConnection(ConnectionString);  
  88.   
  89.         SqlCommand mycommand = new SqlCommand("GetCustomerByEmail",myconnection);  
  90.   
  91.         mycommand.CommandType = CommandType.StoredProcedure;  
  92.   
  93.         SqlParameter parameterCustomerID = new SqlParameter("@CustomerID",SqlDbType.Int,4);  
  94.   
  95.         parameterCustomerID.Direction = ParameterDirection.Output;  
  96.   
  97.         mycommand.Parameters.Add(parameterCustomerID);  
  98.   
  99.         SqlParameter parameterFullName = new SqlParameter("@Name",SqlDbType.NVarChar,50);  
  100.   
  101.         parameterFullName.Direction = ParameterDirection.Output;  
  102.   
  103.         mycommand.Parameters.Add(parameterFullName);  
  104.   
  105.         SqlParameter parameterEmail = new SqlParameter("@Email",SqlDbType.NVarChar,50);  
  106.   
  107.         parameterEmail.Value = Email;  
  108.   
  109.         mycommand.Parameters.Add(parameterEmail);  
  110.   
  111.         SqlParameter parameterPassword = new SqlParameter("@Password",SqlDbType.NVarChar,50);  
  112.   
  113.         parameterPassword.Direction = ParameterDirection.Output;  
  114.   
  115.         mycommand.Parameters.Add(parameterPassword);  
  116.   
  117.         SqlParameter parameterAddress = new SqlParameter("@Address",SqlDbType.NVarChar,255);  
  118.   
  119.         parameterAddress.Direction = ParameterDirection.Output;  
  120.   
  121.         mycommand.Parameters.Add(parameterAddress);  
  122.   
  123.         SqlParameter parameterCountry = new SqlParameter("@Country",SqlDbType.NVarChar,40);  
  124.   
  125.         parameterCountry.Direction = ParameterDirection.Output;  
  126.   
  127.         mycommand.Parameters.Add(parameterCountry);  
  128.   
  129.         SqlParameter parameterPhoneNumber = new SqlParameter("@PhoneNuber",SqlDbType.NVarChar,30);  
  130.   
  131.         parameterPhoneNumber.Direction = ParameterDirection.Output;  
  132.   
  133.         mycommand.Parameters.Add(parameterPhoneNumber);  
  134.   
  135.         SqlParameter parameterFax = new SqlParameter("@Fax",SqlDbType.NVarChar,30);  
  136.   
  137.         parameterFax.Direction = ParameterDirection.Output;  
  138.   
  139.         mycommand.Parameters.Add(parameterFax);  
  140.   
  141.         myconnection.Open();  
  142.   
  143.         mycommand.ExecuteNonQuery();  
  144.   
  145.         myconnection.Close();  
  146.   
  147.         int customerId =(int)(parameterCustomerID.Value);  
  148.   
  149.         if(customerId == 0)  
  150.   
  151.         {  
  152.   
  153.             return null;  
  154.   
  155.         }  
  156.   
  157.         else  
  158.   
  159.         {  
  160.   
  161.             Customer myCustomer = new Customer();  
  162.   
  163.             myCustomer.Name = (string)parameterFullName.Value;  
  164.   
  165.             myCustomer.Password = (string)parameterPassword.Value;  
  166.   
  167.             myCustomer.Email = (string)parameterEmail.Value;  
  168.   
  169.             myCustomer.Addresss = (string)parameterAddress.Value;  
  170.   
  171.             myCustomer.Country = (string)parameterCountry.Value;  
  172.   
  173.             myCustomer.PhoneNumber = (string)parameterPhoneNumber.Value;  
  174.   
  175.             myCustomer.Fax = (string)parameterFax.Value;  
  176.   
  177.             myCustomer.CustomerID = customerId;  
  178.   
  179.             return myCustomer;  
  180.   
  181.         }  
  182.   
  183.             
  184.   
  185.     }  
  186.   
  187.     }  
  188.   
  189. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值