-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm3.cs
More file actions
87 lines (75 loc) · 4.2 KB
/
Copy pathForm3.cs
File metadata and controls
87 lines (75 loc) · 4.2 KB
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
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
namespace Lab06
{
public partial class Form3 : Form
{
// connection string reused by every query
private readonly string connStr;
public Form3()
{
InitializeComponent();
// build connStr once, pointing at lib.accdb in the exe folder
connStr = $"Provider=Microsoft.ACE.OLEDB.12.0;" +
$"Data Source={Path.Combine(Application.StartupPath, "lib.accdb")};";
}
// ───────────────────────────────────────────────────────────
// helper: run a SQL SELECT & show the result in dgvDisplay
// ───────────────────────────────────────────────────────────
private void UpdateGrid(string sql)
{
try
{
DataTable dt = new DataTable();
using OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
da.Fill(dt);
dgvDisplay.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"SQL / Connection error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
// ───────────────────────────────────────────────────────────
// 1️⃣ default view: Users table
// ───────────────────────────────────────────────────────────
private void Form3_Load(object sender, EventArgs e)
{
UpdateGrid("SELECT * FROM Users");
}
// ───────────────────────────────────────────────────────────
// 2️⃣ Show Books Table (sorted by Author ASC)
// ───────────────────────────────────────────────────────────
private void btnShowBook_Click(object sender, EventArgs e)
{
UpdateGrid("SELECT * FROM Books ORDER BY Author");
}
// ───────────────────────────────────────────────────────────
// 3️⃣ Run arbitrary SELECT typed in txtSQL
// ───────────────────────────────────────────────────────────
private void btnSQL_Click(object sender, EventArgs e)
{
string sql = txtSQL.Text.Trim();
if (string.IsNullOrWhiteSpace(sql))
{
MessageBox.Show("Please enter a SQL SELECT statement first.",
"No SQL supplied",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
UpdateGrid(sql);
}
// ───────────────────────────────────────────────────────────
// designer-generated stubs you don’t need right now
// ───────────────────────────────────────────────────────────
private void dgvDisplay_CellContentClick(object sender, DataGridViewCellEventArgs e) { }
private void txtSQL_TextChanged(object sender, EventArgs e) { }
}
}