1.創建數據層
創建對客戶進行操作的存儲過程.存儲過程是數據層的一部分,可以在業務層中訪問它,以實現表示層的某個地方要執行的功能.這裏不再說明把數據訪問邏輯放在單獨一層的優點.
在Store數據庫中創建存儲過程GetCustomerByEmail,該存儲過程把登錄頁面中輸入的EMAIL作爲參數,查找EMAIL地址爲該參數值的客戶,然後以輸出參數的形式返回該客戶的ID,名字,密碼 ,地址,國家,電話和傳真.代碼如下:
-
create procedure GetCustomerByEmail
-
-
(
-
-
@Email nvarchar(50) = null,
-
-
@CustomerID int OUTPUT,
-
-
@Name nvarchar(50) OUTPUT,
-
-
@password nvarchar(50) OUTPUT,
-
-
@Address nvarchar(250) OUTPUT,
-
-
@Country nvarchar(250) OUTPUT,
-
-
@PhoneNumber nvarchar(250) OUTPUT,
-
-
@Fax nvarchar(250) OUTPUT
-
-
)
-
-
AS
-
-
SET NOCOUNT ON
-
-
SELECT
-
-
@CustomerID = c.CutomerID,
-
-
@Name = c.FullName,
-
-
@Password = c.Password,
-
-
@Address = a.Address,
-
-
@Country = a.Country,
-
-
@PhoneNumber = a.PhoneNumber,
-
-
@Fax = a.Fax
-
-
FROM Customers c,Addresses a
-
-
WHERE c.EmailAddree = @Email
-
-
AND a.CustomerId = c.CustomerId
-
-
IF @@Rowcount < 1
-
-
SELECT
-
-
@CustomerID = 0
-
-
GO
-
-
(
-
-
@Email nvarchar(50) = null,
-
-
@CustomerID int OUTPUT,
-
-
@Name nvarchar(50) OUTPUT,
-
-
@password nvarchar(50) OUTPUT,
-
-
@Address nvarchar(250) OUTPUT,
-
-
@Country nvarchar(250) OUTPUT,
-
-
@PhoneNumber nvarchar(250) OUTPUT,
-
-
@Fax nvarchar(250) OUTPUT
-
-
)
-
-
AS
-
-
SET NOCOUNT ON
-
-
SELECT
-
-
@CustomerID = c.CutomerID,
-
-
@Name = c.FullName,
-
-
@Password = c.Password,
-
-
@Address = a.Address,
-
-
@Country = a.Country,
-
-
@PhoneNumber = a.PhoneNumber,
-
-
@Fax = a.Fax
-
-
FROM Customers c,Addresses a
-
-
WHERE c.EmailAddree = @Email
-
-
AND a.CustomerId = c.CustomerId
-
-
IF @@Rowcount < 1
-
-
SELECT
-
-
@CustomerID = 0
-
-
GO
2.創建業務層
業務層是存取數據的類,該類與數據層和表示層(用戶界面)交戶作用,在需要時從表示層獲得數據,並將數據存入數據庫,也可以從數據層獲得數據,並將數據發送給表示層(用戶界面).
(1)在"解決方案資源管理器"中,選擇解決方案下的GoShop項目,右擊,再將鼠標指向"添加",然後選擇"添加類"命令.
(2)在"類別"窗口中選擇"本地項目",然後在"模板"窗口中選擇"代碼文件".在"名稱"文本框中,輸入CustomerDB.
(3)單擊"打開",代碼如下:
-
using System;
-
-
using System.Data;
-
-
using System.Configuration;
-
-
using System.Web;
-
-
using System.Web.Security;
-
-
using System.Web.UI;
-
-
using System.Web.UI.WebControls;
-
-
using System.Web.UI.WebControls.WebParts;
-
-
using System.Web.UI.HtmlControls;
-
-
using System.Data.SqlClient;
-
-
-
-
namespace Goshop{
-
-
-
-
-
-
-
-
-
-
public class Customer
-
-
{
-
-
-
-
public string Name;
-
-
public string Email;
-
-
public string Password;
-
-
public string Country;
-
-
public string Addresss;
-
-
public string PhoneNumber;
-
-
public string Fax;
-
-
public int CustomerID;
-
-
-
-
}
-
-
public class CustomerDB
-
-
{
-
-
private string ConnectionString
-
-
{
-
-
get
-
-
{
-
-
string strConn;
-
-
strConn = "Data Source =(local);";
-
-
strConn +="Initial Catalog = Store;";
-
-
return strConn ;
-
-
}
-
-
}
-
-
public Customer GetCustomerByEmail(string Email)
-
-
{
-
-
SqlConnection myconnection = new SqlConnection(ConnectionString);
-
-
SqlCommand mycommand = new SqlCommand("GetCustomerByEmail",myconnection);
-
-
mycommand.CommandType = CommandType.StoredProcedure;
-
-
SqlParameter parameterCustomerID = new SqlParameter("@CustomerID",SqlDbType.Int,4);
-
-
parameterCustomerID.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterCustomerID);
-
-
SqlParameter parameterFullName = new SqlParameter("@Name",SqlDbType.NVarChar,50);
-
-
parameterFullName.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterFullName);
-
-
SqlParameter parameterEmail = new SqlParameter("@Email",SqlDbType.NVarChar,50);
-
-
parameterEmail.Value = Email;
-
-
mycommand.Parameters.Add(parameterEmail);
-
-
SqlParameter parameterPassword = new SqlParameter("@Password",SqlDbType.NVarChar,50);
-
-
parameterPassword.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterPassword);
-
-
SqlParameter parameterAddress = new SqlParameter("@Address",SqlDbType.NVarChar,255);
-
-
parameterAddress.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterAddress);
-
-
SqlParameter parameterCountry = new SqlParameter("@Country",SqlDbType.NVarChar,40);
-
-
parameterCountry.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterCountry);
-
-
SqlParameter parameterPhoneNumber = new SqlParameter("@PhoneNuber",SqlDbType.NVarChar,30);
-
-
parameterPhoneNumber.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterPhoneNumber);
-
-
SqlParameter parameterFax = new SqlParameter("@Fax",SqlDbType.NVarChar,30);
-
-
parameterFax.Direction = ParameterDirection.Output;
-
-
mycommand.Parameters.Add(parameterFax);
-
-
myconnection.Open();
-
-
mycommand.ExecuteNonQuery();
-
-
myconnection.Close();
-
-
int customerId =(int)(parameterCustomerID.Value);
-
-
if(customerId == 0)
-
-
{
-
-
return null;
-
-
}
-
-
else
-
-
{
-
-
Customer myCustomer = new Customer();
-
-
myCustomer.Name = (string)parameterFullName.Value;
-
-
myCustomer.Password = (string)parameterPassword.Value;
-
-
myCustomer.Email = (string)parameterEmail.Value;
-
-
myCustomer.Addresss = (string)parameterAddress.Value;
-
-
myCustomer.Country = (string)parameterCountry.Value;
-
-
myCustomer.PhoneNumber = (string)parameterPhoneNumber.Value;
-
-
myCustomer.Fax = (string)parameterFax.Value;
-
-
myCustomer.CustomerID = customerId;
-
-
return myCustomer;
-
-
}
-
-
-
-
}
-
-
}
-
-
}
|