-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
150 lines (114 loc) · 4.47 KB
/
Form1.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SQLTester
{
public partial class testerForm : Form
{
Button[] buttons = new Button[26];
OleDbConnection connection;
string sqlQuery = "SELECT a.Author, t.Title, p.Name " +
"FROM Authors as a, Titles as t, Publishers as p, Title_Author as ta " +
"WHERE a.AU_ID = ta.AU_ID " +
"AND t.ISBN = ta.ISBN " +
"AND t.PubID = p.PubID";
public testerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string conString = Helper.ConVal("Books");
connection =new OleDbConnection(conString);
connection.Open();
int btnWidth, btnLeft, btnTop;
int btnHeight = 30;
btnWidth = ClientSize.Width / 13; //less than the actual width because integer is used
btnLeft= ClientSize.Width - (btnWidth*13);
btnTop = commandText.Top + commandText.Height+ 16;
for(int i=0; i< 26; i++)
{
buttons[i] =new Button();
buttons[i].Text = ((char)(65+i)).ToString();
buttons[i].Width= btnWidth;
buttons[i].Height= btnHeight;
buttons[i].Left= btnLeft;
buttons[i].Top= btnTop;
Controls.Add(buttons[i]);
buttons[i].Click += new EventHandler(btnSqlClick);
btnLeft += btnWidth;
if (i == 12)
{
btnLeft = ClientSize.Width - (btnWidth * 13);//reset it to the start
btnTop +=btnHeight;
}
}
}
private void frmClosing(object sender, FormClosingEventArgs e)
{
connection.Close(); //close the connection when the form is closing
connection.Dispose(); //delete the objects
}
private void btnSqlClick(object sender, EventArgs e)
{
OleDbCommand cmd = null; ;
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable booksTable = new DataTable();
Button clickedButton = (Button)sender;
const string showRecords = "ShowRecords";
string sqlStatement;
switch (clickedButton.Text)
{
case showRecords:
sqlStatement = sqlQuery;
break;
default:
sqlStatement = sqlQuery + " AND a.Author like '" + clickedButton.Text + "%' ";
break;
}
try
{
cmd = new OleDbCommand(sqlStatement, connection);
adapter.SelectCommand = cmd;
adapter.Fill(booksTable); //holds the result of the query
recordGridView.DataSource = booksTable; //populate the gridview
countLabel.Text = booksTable.Rows.Count.ToString(); //counts the number of rows
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error in SQL Command", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
cmd.Dispose();//delete the object
adapter.Dispose();
booksTable.Dispose();
}
private void executeButton_Click(object sender, EventArgs e)
{
OleDbCommand cmd = null; ;
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable booksTable = new DataTable();
try
{
cmd = new OleDbCommand(commandText.Text, connection);
adapter.SelectCommand = cmd;
adapter.Fill(booksTable); //holds the result of the query
recordGridView.DataSource = booksTable; //populate the gridview
countLabel.Text = booksTable.Rows.Count.ToString(); //counts the number of rows
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error in SQL Command", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
cmd.Dispose();//delete the object
adapter.Dispose();
booksTable.Dispose();
}
}
}