因此,我試圖創建一個名爲「shoutout」的區域,當前用戶可以查看「shoutout」,基本上來自shoutout表的消息與他/她的朋友。循環WHERE變量在SELECT語句中的c#
因此,我所做的首先將所有當前用戶的朋友放入一個名爲「currentuserfriends」的數組中,然後我希望從shoutout表中選擇消息,其中username =「currentuserfriends」中的所有用戶。
但是我不知道我不希望如何循環在SELECT語句中,
不得不做這樣的事情,
string getuserfriendlist = "SELECT friend FROM friend WHERE username = '" + currentuserfriends[0] + "'";
和[0]手動增加currentuserfriends。有沒有一種方法可以在單個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];
我想報告一個錯誤,我創建了一個用戶帳戶,現在您的網站停止工作。我的用戶名是'';放桌吶喊; - (請注意使用SqlParameters,做一個搜索,這個網站上有很多關於它的問題。) –
)))))))))SQL注入使我的一天! – Maris
關於主題筆記,[這裏是一個不同但相關的問題](http://stackoverflow.com/questions/17321281/is-there-a-better-way-to-dynamically-build-sql-where - 條款比使用1-1在),可以解決你的問題。 **編輯:**另一方面的問題,什麼是與大小9999陣列?爲什麼不使用'List'? –