私は SQL に非常に不慣れで、1 回しかレッスンを受けていません。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 を作成して結果セットに追加することはできません。