考:清華大學出版社三層結構


1.創建數據層

   創建對客戶進行操作的存儲過程.存儲過程是數據層的一部分,可以在業務層中訪問它,以實現表示層的某個地方要執行的功能.這裏不再說明把數據訪問邏輯放在單獨一層的優點.

   在Store數據庫中創建存儲過程GetCustomerByEmail,該存儲過程把登錄頁面中輸入的EMAIL作爲參數,查找EMAIL地址爲該參數值的客戶,然後以輸出參數的形式返回該客戶的ID,名字,密碼 ,地址,國家,電話和傳真.代碼如下:

[csharp] view plaincopy
  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 plaincopy
  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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章