因此,我正在尝试创建一个名为“shoutout”的区域,当前用户可以在其中查看“shoutout”,基本上是来自与他/她是朋友的人的shoutout 表中的消息。
所以,我首先将所有当前用户的朋友放入一个名为“currentuserfriends”的数组中,然后我希望从用户名=“currentuserfriends”中的所有用户的喊出表中选择消息。
但是我不知道如何在 select 语句中循环,
我不想做类似的事情,
string getuserfriendlist = "SELECT friend FROM friend WHERE username = '" + currentuserfriends[0] + "'";
并手动增加 currentuserfriends[0]。有没有办法在单个 select 语句中循环 currentuserfriends 直到结束?谢谢。
朋友桌
username | friend
大喊表
username | message | datetime
代码:
string[] currentuserfriends = new string[9999];
string[] posternames = new string[9999];
string getuserfriendlist = "SELECT friend FROM friend WHERE username = '" + currentuser + "'";
SqlCommand cmdb = new SqlCommand(getuserfriendlist, con);
SqlDataReader drb;
drb = cmdb.ExecuteReader();
while (drb.Read())
{
string currentuserfriend = drb["friend"].ToString();
currentuserfriends[l++] = currentuserfriend;
}
drb.Close();
string getshoutout = "SELECT * FROM shoutout WHERE username = '" + currentuserfriends + "' AND username ='" + currentuser + "' order by datetime DESC";
SqlCommand cmdc = new SqlCommand(getshoutout, con);
SqlDataReader drc;
drc = cmdc.ExecuteReader();
while (drc.Read())
{
string postername = drb["username"].ToString();
posternames[m++] = postername;
}
drc.Close();
Label2.Text = posternames[0];
Label3.Text = posternames[1];
Label4.Text = posternames[2];
Label5.Text = posternames[3];
Label6.Text = posternames[4];