基本上,當在另一個頁面中有人選擇一個類別時,它會將他重定向到該頁面并將“?category =(他選擇的類別)”添加到 url,然后在此頁面中檢查他的類別是什么,并從類別所在的 db 中提取 id等于它選擇的,然后它選擇一個隨機的id。
Uri uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
string querystring = HttpUtility.ParseQueryString(uri.Query).Get("category");
SqlCommand cmd = new SqlCommand();
List<int> idList = new List<int>();
SqlConnection con = new SqlConnection(@"Data source");
// I deleted the Data source but it's in the code
try
{
cmd = new SqlCommand($"SELECT Id FROM Facts WHERE category='{querystring}'", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
int i = 0;
while ((bool)reader.Read())
{
idList.Add((int)reader.GetValue(0));
i ;
}
reader.Close();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
Random rnd = new Random();
int[] idArray = idList.ToArray();
int randIndex = rnd.Next(idArray.Length);
int random = idArray[randIndex];
string SQL = $"SELECT facts FROM Facts WHERE id='{random}';";
cmd = new SqlCommand(SQL, con);
con.Open();
object scalar = cmd.ExecuteScalar();
con.Close();
這是代碼,有時它可以作業,有時它會拋出“索引超出了陣列的范圍。”
真的需要幫助,已經作業了很長時間,我不知道。
uj5u.com熱心網友回復:
使用這里帶來的一些想法和答案,我設法修復它,首先我洗掉了未使用和不必要的代碼(感謝一些建議)并使用除錯代碼System.Diagnostics.Debug.WriteLine();然后我獲取了獲取 ID 的整個代碼并制作了一個方法它 -
List<int> GetData(string querystring)
{
// This method receives the URL query parameter of the category and outputs a list of all the fact ids in the category .
SqlCommand cmd = new SqlCommand();
List<int> idList = new List<int>();
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RotemCohen\source\repos\firstsite\firstsite\App_Data\db.mdf;Integrated Security=True");
try
{
cmd = new SqlCommand($"SELECT Id FROM Facts WHERE category='{querystring}'", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
int i = 0;
while (reader.Read())
{
idList.Add((int)reader.GetValue(0));
i ;
}
reader.Close();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return idList;
}
這部分獲取事實 id 并選擇一個隨機的,然后將其輸出到 HTML:
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RotemCohen\source\repos\firstsite\firstsite\App_Data\db.mdf;Integrated Security=True");
Uri uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
string category = HttpUtility.ParseQueryString(uri.Query).Get("category");
List<int> listID = GetData(category);
Random rnd = new Random();
int random = rnd.Next(0, listID.Count);
int randomFactID = listID[random];
string SQLQuery = $"SELECT facts FROM Facts WHERE id='{randomFactID}';";
cmd = new SqlCommand(SQLQuery, con);
con.Open();
object randomFact = cmd.ExecuteScalar();
con.Close();
注意:這很容易受到 SQL 注入攻擊,在沒有先引數化變數的情況下不要使用它SQLQuery!看到這個來了解它。
uj5u.com熱心網友回復:
可能有必要管理沒有結果的情況:
...
if (idList.Count > 0)
{
Random rnd = new Random();
int[] idArray = idList.ToArray();
int randIndex = rnd.Next(idArray.Length);
int random = idArray[randIndex];
...
uj5u.com熱心網友回復:
如果失敗:
int randIndex = rnd.Next(idArray.Length);
int random = idArray[randIndex];
然后:
idArray被共享并在操作之間交換- 陣列為空
我們可以排除 1,因為它是本地的,而不是欄位;因此問題是 2
(我不包括Random.Next被打破的第三種選擇,因為這似乎不太可能)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511414.html
