我使用簡單的分頁形式設置了此C#查詢,但其執行的某些查詢可能有數百萬行。我最近使用各種參數對其進行了測試,其中一些查詢在超過200,000條記錄時超時。我怎麼能限制讀者每次讀〜50,000行?Rest API API查詢超出行數太多,如何限制讀取器讀取太多行
public DataTable GetTranReport(string aliasName, string pageString, string year, string month)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("recid"));
dataTable.Columns.Add(new DataColumn("folder"));
dataTable.Columns.Add(new DataColumn("cust"));
dataTable.Columns.Add(new DataColumn("direction"));
//pagination variables (pageString must be 1+ in order to represent current page)
int pageInt;
Int32.TryParse(pageString, out pageInt);
if (dbConnection5.State.ToString() != "Open")
{
dbConnection5.Open();
}
int itemNum = 0;
string selecteddate = string.Format("[" + year + month + "]");
string query = string.Format("SELECT recid, folder, cust, direction FROM " + selecteddate + " WHERE cust = @aliasname order by thedate DESC;");
SqlCommand command = new SqlCommand(query, dbConnection5);
command.Parameters.AddWithValue("@selecteddate", selecteddate);
command.Parameters.AddWithValue("@aliasname", aliasname);
SqlDataReader reader = command.ExecuteReader();
int i = 0;
DataRow newTRRow;
if (reader.HasRows)
{
while (reader.Read())
{
++i;
if (pageInt > 1)
{
if (i >= ((pageInt * 10) - 10) && i < (10 * pageInt))
{
itemNum += 1;
string itemString = string.Format("itemString" + itemNum);
newTRRow = dataTable.NewRow();
newTRRow["recid"] = reader["recid"];
newTRRow["folder"] = reader["folder"];
newTRRow["customer"] = reader["customer"];
newTRRow["direction"] = reader["direction"];
dataTable.Rows.Add(newTRRow);
}
}
else
{
if (itemNum < 10)
{
itemNum += 1;
string itemString = string.Format("itemString" + itemNum);
newTRRow = dataTable.NewRow();
newTRRow["recid"] = reader["recid"];
newTRRow["folder"] = reader["folder"];
newTRRow["customer"] = reader["customer"];
newTRRow["direction"] = reader["direction"];
dataTable.Rows.Add(newTRRow);
}
}
}
}
dataTable.Rows.Add(string.Format("Total number of records is: {0}", i));
reader.Close();
dbConnection5.Close();
return dataTable;
}
謝謝,這工作完美。 – 2015-04-04 02:55:17
歡迎你 – jfun 2015-04-04 07:49:16