-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathManualQueryWithReadOnlyForm.cs
79 lines (70 loc) · 2.64 KB
/
ManualQueryWithReadOnlyForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Data.Common;
using System.Data.SQLite;
using System.Windows.Forms;
using Code4Bugs.SimpleDataGridViewPaging.Statement;
using Code4Bugs.SimpleDataGridViewPaging.Statement.Builder;
namespace Code4Bugs.SimpleDataGridViewPaging.Examples
{
public partial class ManualQueryWithReadOnlyForm : Form
{
public ManualQueryWithReadOnlyForm()
{
InitializeComponent();
var rowsStatementBuilder = new RowsStatementBuilder();
rowsStatementBuilder.CommandText("SELECT * FROM tracks LIMIT {1} OFFSET {2}");
dataGridViewPaging1.DbRequestHandler = new DbRequestHandler
{
Connection = new SQLiteConnection("Data Source=chinook.db"),
CountStatementBuilder = new CustomCountStatementBuilder(),
RowsStatementBuilder = new CustomRowsStatementBuilder()
};
}
private class CustomCountStatementBuilder : CountStatementBuilder
{
public override IStatement<int> Build()
{
return new CountStatement {Connection = _connection};
}
private class CountStatement : IStatement<int>
{
public DbConnection Connection { get; set; }
public int Execute()
{
using (var command = Connection.CreateCommand())
{
command.CommandText = "SELECT COUNT(*) FROM tracks";
var result = command.ExecuteScalar();
return Convert.ToInt32(result);
}
}
}
}
private class CustomRowsStatementBuilder : RowsStatementBuilder
{
public override IStatement<object> Build()
{
return new RowsStatement
{
Connection = _connection,
MaxRecords = _maxRecords,
PageOffset = _pageOffset
};
}
private class RowsStatement : IStatement<object>
{
public DbConnection Connection { get; set; }
public int MaxRecords { get; set; }
public int PageOffset { get; set; }
public object Execute()
{
using (var command = Connection.CreateCommand())
{
command.CommandText = $"SELECT * FROM tracks LIMIT {MaxRecords} OFFSET {PageOffset}";
return command.ExecuteReader();
}
}
}
}
}
}