欢迎大家关注我日志,今天总结下控制台项目中的一些问题:
1. 多表连接:
首先建个外键表,在进行两表或三表连接
如下购物车的三表连接:
select users.name,commodity.name,commodity.number from users_commodity
inner join users on users_commodity .uid=users.id
inner join commodity on users_commodity.cid=commodity .id;
2.Arrylist和迭代器的用法:
ArrayList list2 = new ArrayList();
public ArrayList Show()
{
SqlConnection con = new SqlConnection("server=.;database=gouwu;Trusted_Connection=SSPI");
con.Open();
string sql = "select users.name,commodity.name,commodity.number from users_commodity "+
"inner join users on users_commodity .uid=users.id "+
"inner join commodity on users_commodity.cid=commodity .id ";//多表连接
SqlCommand sc = new SqlCommand(sql, con);
SqlDataReader reader = sc.ExecuteReader();
while (reader.Read())
{
Users u = new Users();
u.Name = (string)reader.GetValue(0);
Commodity c = new Commodity();
c.Name = (string)reader.GetValue(1);
c.Number = (int)reader.GetValue(2);
list2.Add(u);
list2.Add(c);
}
Return list;
public void select() {
ArrayList list2 = new Users_Commodity().Show();
IEnumerator it = list2.GetEnumerator();//迭代器
while (it.MoveNext ()){
Users u= (Users)it.Current;
it.MoveNext();
Commodity c = (Commodity)it.Current;
Console.WriteLine(u.Name +" "+c.Name +" "+c.Number );
更多精彩可以关注我们的狗刨网: