C# DataReader从2个类获取数据时出现问题
我对SQL非常缺乏经验,只上过一堂课。我试图弄清楚如何使用DataReader从两个类中获取信息 我的客户类别:C# DataReader从2个类获取数据时出现问题,c#,sql,sql-server,sqldatareader,C#,Sql,Sql Server,Sqldatareader,我对SQL非常缺乏经验,只上过一堂课。我试图弄清楚如何使用DataReader从两个类中获取信息 我的客户类别: public class Customer { private CreditCard _creditCard; private List<Subscription> _subscriptions; private int _customerId; private string _name; private string _addre
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();
}
}
公共类客户
{
个人信用卡;
私人列表订阅;
私人int_客户ID;
私有字符串\u名称;
私有字符串地址;
专用内码;
私人字符串(城市),;
私人字符串\u电子邮件;
私有字符串\u密码;
公共客户(整数id、字符串名称、字符串电子邮件、字符串密码)
{
这个。_customerId=id;
这个。_name=name;
这个。_email=电子邮件;
此密码为.\u password=password;
这是。_subscriptions=new List();
}
公共客户(整数id、字符串名称)
{
这个。_customerId=id;
这个。_name=name;
_订阅=新列表();
}
公共重写字符串ToString()
{
返回\u customerId++\u名称;
}
公共int客户ID
{
获取{return\u customerId;}
设置{u customerId=value;}
}
公共字符串名
{
获取{return\u name;}
设置{u name=value;}
}
公共字符串地址
{
获取{return\u address;}
设置{u address=value;}
}
公共整数ZipCode
{
获取{return\u zipCode;}
设置{u zipCode=value;}
}
公共字符串城市
{
获取{return\u city;}
设置{u city=value;}
}
公共字符串电子邮件
{
获取{return\u email;}
设置{u email=value;}
}
公共字符串密码
{
获取{return\u password;}
设置{u password=value;}
}
公共列表订阅
{
获取{返回_订阅;}
}
公开无效添加订阅(个订阅)
{
_订阅。添加;
}
公共订阅FindSubscription(int sId)
{
bool-found=false;
int i=0;
而(!找到和(&i)<\u subscriptions.Count)
{
if(_subscriptions[i].SubscriptionId==sId)
发现=真;
其他的
i++;
}
如果(找到)
返回订阅[i];
其他的
返回新订阅();
}
}
我的订阅类:
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;
}
}
公共类订阅
{
私有订阅类型_SubscriptionType;
私人卡莫代尔(CarModel);;
私人列表预订;
私有int_subscriptionId;
公共订阅(int subscriptionId、SubscriptionType SubscriptionType、CarModel CarModel)
{
这个._subscriptionId=subscriptionId;
这是。_subscriptionType=subscriptionType;
这个。_carModel=carModel;
_预订=新列表();
}
公开认购()
{ }
公共int订阅ID
{
获取{return\u subscriptionId;}
设置{u subscriptionId=value;}
}
公共订阅类型订阅类型
{
获取{return\u subscriptionType;}
}
公共CarModel CarModel
{
获取{return\u carModel;}
}
公共重写字符串ToString()
{
返回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;
}
公共类DBCustomer
{
私有静态SqlCommand dbCmd=null;
公共静态列表GetCustomers()
{
List returnList=新列表();
string sql=“选择*来自客户”;
dbCmd=AccessDBSQLClient.GetDbCommand(sql);
IDataReader dbReader;
dbReader=dbCmd.ExecuteReader();
客户c;
while(dbReader.Read())
{
c=新客户(转换为.ToInt32(dbReader[“cId”].ToString()),dbReader[“cName”].ToString());
返回名单。添加(c);
}
AccessDBSQLClient.Close();
退货清单;
}
公共静态列表GetCustomersByEmail(字符串电子邮件)
{
List returnList=新列表();
string sql=“从客户中选择*,其中cEmail=”+email+”;
dbCmd=AccessDBSQLClient.GetDbCommand(sql);
IDataReader dbReader;
dbReader=dbCmd.ExecuteReader();
客户c;
while(dbReader.Read())
{
c=新客户(转换为.ToInt32(dbReader[“cId”].ToString()),dbReader[“cName”].ToString());
返回名单。添加(c);
}
AccessDBSQLClient.Close();
退货清单;
}
公共静态列表GetCustomerSubscriptions(int-cId)
{
List returnList=新列表();
string sql=“选择S.sId、ST.stName、CM.cmddescription”
+来自客户C、订阅S、订阅类型ST、CarModel CM
+“其中C.cId=S.cId”
+“S.stId=ST.stId”
+“和S.cmId=CM.cmId”
+“和C.cId=“+cId;
dbCmd=AccessDBSQLClient.GetDbCommand(sql);
IDataReader dbReader;
dbReader=dbCmd.ExecuteReader();
认购书;
while(dbReader.Read())
{
//s=新订阅();
s=新订阅((int)(dbReader[“sId”])。ToString(),
dbReader[“stName”],
dbReader[“cmddescription”]);
返回列表。添加(s);
}
AccessDBSQLClient.Close();
退货清单;
}
显然,GetCustomers和GetCustomersByEmail方法正在工作,但我不知道如何创建GetCustomerSubscriptions方法,因为它必须返回fra 2类的数据,我不能只创建一个客户来添加到结果集。你能在代码中注释你正在尝试做什么和什么不起作用吗?每个客户都有一个
List
作为属性。因此您应该创建一个客户(我假设参数cid
是客户id),并在GetCustomerSubscriptions
中使用subscriptions初始化它,然后返回该指令