connection.Open();
var cmd = new SqlCommand("SELECT TOP 1000 [Id] ,[Name] FROM [SomeBase]", connection); //Here
context.MaxID = 100;
int en = 5; //Maximum number of rows in the slice
int iter = 0; //Iterator for each single row in the slice
try
{
SqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
{
while (iter < en) //I need 1-5 rows in first iteration, 6-10 in second...
{
iter++;
reader.Read();
context.TextLog += String.Format("{0}\t{1}\n",
reader.GetInt64(0),
reader.GetString(1));
}
}
reader.Close();
}
我試圖從結果中獲取片的行。 我期望:前五個,然後退出腳本。下一個開始將是第二個五個結果(6-10)等。 我如何管理它,例如使用MaxID或某種迭代器。如何用SQLReader使用.NET讀取片段查詢結果
告訴數據庫服務器,您只需要那些帶有LIMIT/FETCH和OFFSET的部分。不要告訴它你想要1000個結果,只是扔掉它們。 –
[在SQL Server中分頁結果的最佳方式是什麼]的可能重複(http://stackoverflow.com/questions/109232/what-is-the-best-way-to-paginate-results-in-sql-服務器) – bbsimonbb