我对 SQL 非常缺乏经验,只上过一节课。我试图弄清楚如何使用 DataReader 从 2 个类中获取信息。
我的客户类别:
public class Customer
{
private CreditCard _creditCard;
private List<Subscription> _subscriptions;
private int _customerId;
private string _name;
private string _address;
private int _zipCode;
private string _city;
private string _email;
private string _password;
public Customer(int id, string name, string email, string password)
{
this._customerId = id;
this._name = name;
this._email = email;
this._password = password;
this._subscriptions = new List<Subscription>();
}
public Customer(int id, string name)
{
this._customerId = id;
this._name = name;
_subscriptions = new List<Subscription>();
}
public override string ToString()
{
return _customerId + " " + _name;
}
public int CustomerId
{
get { return _customerId; }
set { _customerId = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public int ZipCode
{
get { return _zipCode; }
set { _zipCode = value; }
}
public string City
{
get { return _city; }
set { _city = value; }
}
public string Email
{
get { return _email; }
set { _email = value; }
}
public string Password
{
get { return _password; }
set { _password = value; }
}
public List<Subscription> Subscriptions
{
get { return _subscriptions; }
}
public void AddSubscription(Subscription s)
{
_subscriptions.Add(s);
}
public Subscription FindSubscription(int sId)
{
bool found = false;
int i = 0;
while (!found && i < _subscriptions.Count)
{
if (_subscriptions[i].SubscriptionId == sId)
found = true;
else
i++;
}
if (found)
return _subscriptions[i];
else
return new Subscription();
}
}
我的订阅课程:
public class Subscription
{
private SubscriptionType _subscriptionType;
private CarModel _carModel;
private List<Booking> _bookings;
private int _subscriptionId;
public Subscription(int subscriptionId, SubscriptionType subscriptionType, CarModel carModel)
{
this._subscriptionId = subscriptionId;
this._subscriptionType = subscriptionType;
this._carModel = carModel;
_bookings = new List<Booking>();
}
public Subscription()
{ }
public int SubscriptionId
{
get { return _subscriptionId; }
set { _subscriptionId = value; }
}
public SubscriptionType SubscriptionType
{
get { return _subscriptionType; }
}
public CarModel CarModel
{
get { return _carModel; }
}
public override string ToString()
{
return this.SubscriptionId + "\t " + this.SubscriptionType + "\t " + this.CarModel;
}
}
我的数据库类:
public class DBCustomer
{
private static SqlCommand dbCmd = null;
public static List<Customer> GetCustomers()
{
List<Customer> returnList = new List<Customer>();
string sql = "select * from Customer";
dbCmd = AccessDBSQLClient.GetDbCommand(sql);
IDataReader dbReader;
dbReader = dbCmd.ExecuteReader();
Customer c;
while (dbReader.Read())
{
c = new Customer(Convert.ToInt32(dbReader["cId"].ToString()), dbReader["cName"].ToString());
returnList.Add(c);
}
AccessDBSQLClient.Close();
return returnList;
}
public static List<Customer> GetCustomersByEmail(string email)
{
List<Customer> returnList = new List<Customer>();
string sql = "select * from Customer where cEmail = '" + email+"'";
dbCmd = AccessDBSQLClient.GetDbCommand(sql);
IDataReader dbReader;
dbReader = dbCmd.ExecuteReader();
Customer c;
while (dbReader.Read())
{
c = new Customer(Convert.ToInt32(dbReader["cId"].ToString()), dbReader["cName"].ToString());
returnList.Add(c);
}
AccessDBSQLClient.Close();
return returnList;
}
public static List<Subscription> GetCustomerSubscriptions(int cId)
{
List<Subscription> returnList = new List<Subscription>();
string sql = "select S.sId, ST.stName,CM.cmDescription "
+ "from Customer C, Subscription S, SubscriptionType ST, CarModel CM "
+ "where C.cId = S.cId "
+ "and S.stId = ST.stId "
+ "and S.cmId = CM.cmId "
+ "and C.cId = " + cId;
dbCmd = AccessDBSQLClient.GetDbCommand(sql);
IDataReader dbReader;
dbReader = dbCmd.ExecuteReader();
Subscription s;
while (dbReader.Read())
{
//s = new Subscription();
s = new Subscription((int)(dbReader["sId"]).ToString(),
dbReader["stName"],
dbReader["cmDescription"]);
returnList.Add(s);
}
AccessDBSQLClient.Close();
return returnList;
}
显然 GetCustomers 和 GetCustomersByEmail 方法正在工作,但我不知道如何制作 GetCustomerSubscriptions 方法,因为它必须返回 2 个类的数据,而且我不能只构建一个 Customer 添加到结果集中。