private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int kx = 25;
int ky = 50;
con.Close();
con.Open();
String sql = "SELECT * from cash_bill";
cmd = new OleDbCommand(sql, con);
dr1 = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr1.Read())
{
e.Graphics.DrawString(dr1[7].ToString(), printFont, Brushes.Black, kx + 75, ky + 5);
if (itemperpage < 29)
{
itemperpage += 1;
e.HasMorePages = false;
ky = ky + 50;
}
else
{
itemperpage = 0;
e.HasMorePages = true;
return;
}
}
con.Close();
}
- 尝试使用语句而不是打开和关闭连接 每次。
- 不要试图将所有cash_bill行放在一个行中 查询。尝试 Top(( 关键字来限制结果。
- 优化查询 通过在查询中添加 ID> X 条件。
- 生成查询 并在 while 循环内进行调用。并更新 X 参数按查询结果中的最大 id 值。
示例查询:
字符串查询 = 字符串。Format("SELECT TOP({0}( from cash_bill where id> {1}", maxRowPerPage, maxRowId(;
在每个循环结束时更新 maxRowId。(PS:我假设值是按 ID 排序的(
这种方法将减少代码的时间和代码复杂性,并提高使用 sql 连接的效率。
此外,您可能会考虑使用多个线程。由于通过此改进,您将通过单个查询收集单个页面的数据,因此您可以同时操作和打印数据。
希望这有帮助。