📄 customerdb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using MobileOnlineShop.Entities;
using System.Configuration;
namespace MobileOnlineShop.DAL
{
/// <summary>
/// CustomerDB 的摘要说明。
/// </summary>
public class CustomerDB
{ private static string connStr= "server=(local);database=shopping;uid=sa;pwd=sa;";
/// <summary>
/// 添加一个新用户(注册用户)
/// </summary>
/// <param name="objCustomer"></param>
/// <returns></returns>
public static int AddNewCustomer(Customers objCustomer)
{
SqlConnection conn = new SqlConnection(connStr);
//调用存储过程,添加一个新用户
SqlCommand comm = new SqlCommand("AddCustomer",conn);
comm.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@name",SqlDbType.NVarChar);
SqlParameter p2 =new SqlParameter("@email",SqlDbType.NVarChar);
SqlParameter p3 = new SqlParameter("@passWord",SqlDbType.NVarChar);
SqlParameter p4 =new SqlParameter("@phoneNumber",SqlDbType.NVarChar);
SqlParameter p5 = new SqlParameter("@address",SqlDbType.NVarChar);
p1.Value = objCustomer.Name;
p2.Value = objCustomer.Email;
p3.Value = objCustomer.PassWord;
p4.Value = objCustomer.PhoneNumber;
p5.Value = objCustomer.Address;
comm.Parameters.Add(p1);
comm.Parameters.Add(p2);
comm.Parameters.Add(p3);
comm.Parameters.Add(p4);
comm.Parameters.Add(p5);
conn.Open();
//返回插入记录的CustomerID
int id = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
return id;
}
/// <summary>
/// 验证是否有相同的Email
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public static int Valide(string email)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("GetValidCustomerID",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@email",SqlDbType.NVarChar);
p.Value = email;
cmd.Parameters.Add(p);
conn.Open();
int id = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
return id;
}
/// <summary>
/// 用户登录
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
/// <returns></returns>
public static int Login(string email,string password)
{
SqlConnection conn=new SqlConnection(connStr);
SqlCommand comm=new SqlCommand("GetCustomerID",conn);
comm.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@customerID",SqlDbType.Int);
p1.Direction = ParameterDirection.Output;
comm.Parameters.Add(p1);
comm.Parameters.Add("@email",email);
comm.Parameters.Add("@password",password);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
return (int)p1.Value;
}
/// <summary>
/// 通过用户ID,得到用户的详细信息
/// </summary>
/// <param name="customerID"></param>
/// <returns></returns>
public static Customers GetCustomerDetails(int customerID)
{
MobileOnlineShop.Entities.Customers newCustomer=new Customers();
SqlConnection conn=new SqlConnection(connStr);
SqlCommand comm=new SqlCommand("GetCustomerDetail",conn);
comm.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@customerID", SqlDbType.Int);
p.Value = customerID;
comm.Parameters.Add(p);
conn.Open();
SqlDataReader dr;
dr = comm.ExecuteReader();
if(dr.Read())
{
newCustomer.Name=dr.GetString(0);
newCustomer.Email=dr.GetString(1);
newCustomer.PassWord=dr.GetString(2);
newCustomer.PhoneNumber=dr.GetString(3);
newCustomer.Address=dr.GetString(4);
}
conn.Close();
return newCustomer;
}
/// <summary>
/// 修改个人信息
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
public static int UpdateCustomers(int customerID,Customers objCustomers)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand comm = new SqlCommand("ModifyCustomers",conn);
comm.CommandType = CommandType.StoredProcedure;
//创建参数
SqlParameter p1 = new SqlParameter("@name",SqlDbType.NVarChar);
SqlParameter p3 = new SqlParameter("@password",SqlDbType.NVarChar);
SqlParameter p4 = new SqlParameter("@phonenumber",SqlDbType.NVarChar);
SqlParameter p5 = new SqlParameter("@address",SqlDbType.NVarChar);
SqlParameter p6 = new SqlParameter("@customerID",SqlDbType.Int);
p1.Value = objCustomers.Name;
p3.Value = objCustomers.PassWord;
p4.Value = objCustomers.PhoneNumber;
p5.Value = objCustomers.Address;
p6.Value = customerID;
comm.Parameters.Add(p1);
comm.Parameters.Add(p3);
comm.Parameters.Add(p4);
comm.Parameters.Add(p5);
comm.Parameters.Add(p6);
conn.Open();
//执行命令
int s = comm.ExecuteNonQuery();
conn.Close();
return s;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -