我有一个包含两个表的数据库,一个用于登录,另一个用于购买的游戏。我创建了一个登录会话,现在我无法显示这个特定客户的所有行。我可以从MS Access查看表关系,但我不知道如何在标签中显示它们。
这是我的编码:
OleDbDataReader myReader = null;
OleDbCommand cmd = new OleDbCommand("select * from new_table, games where new_table.MemberID='" + Session["New"] + "'", con);
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
label_id.Text = (myReader["MemberID"].ToString()); //welcome user
}
OleDbDataReader myReader2 = null;
OleDbCommand cmd2 = new OleDbCommand("select * from new_table, games where '" + Session["New"] + "' = games.custID", con);
myReader2 = cmd2.ExecuteReader();
if (myReader2.HasRows)
{
DataTable dt = new DataTable();
dt.Load(myReader2);
label1.Text = dt.Rows[0]["gameTitle"].ToString();
label2.Text = "$" + Convert.ToDecimal(dt.Rows[0]["gamePrice"]).ToString("N2");
label3.Text = "$" + Convert.ToDecimal(dt.Rows[0]["gameDisc"]).ToString("N2");
label4.Text = dt.Rows[2]["gameTitle"].ToString();
label7.Text = dt.Rows[4]["gameTitle"].ToString();
}
我的问题是,如果我设置dt.Rows[3]
,则会显示错误System.IndexOutOfRangeException: There is no row at position 3.
。我仍然是C#的初学者,如果之前发布的话,对不起。抱歉我的英语不好,这不是我的第一语言。
答案 0 :(得分:0)
尝试此查询:
string query = "select * from (games
inner join new_table on games.custID = new_table.MemberID)
where new_table.MemberID = '" + Session["New"] + "'";