001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Data.OleDb;
006.
using
System.Drawing;
007.
using
System.Linq;
008.
using
System.Text;
009.
using
System.Threading.Tasks;
010.
using
System.Windows.Forms;
011.
012.
namespace
SchoolApp
013.
{
014.
public
partial
class
Form1 : Form
015.
{
016.
private
OleDbConnection connection =
new
OleDbConnection();
017.
public
Form1()
018.
{
019.
InitializeComponent();
020.
try
{
021.
connection.ConnectionString = @
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|datadirectory|\Database.mdb"
;
022.
connection.Open();
023.
label1.Text =
"Connection Success"
;
024.
connection.Close();
025.
026.
}
027.
catch
(Exception ex)
028.
{
029.
MessageBox.Show(
"Error"
+ ex);
030.
031.
}
032.
}
033.
034.
private
void
btnSave_Click(
object
sender, EventArgs e)
035.
{
036.
037.
try
{
038.
connection.Open();
039.
OleDbCommand command =
new
OleDbCommand();
040.
command.Connection = connection;
041.
command.CommandText =
"insert into Student (Title,Fname,Lname,Bev) values('"
+textBox1.Text.Trim()+
"','"
+ textBox2.Text.Trim() +
"','"
+ textBox3.Text.Trim() +
"','"
+ textBox4.Text.Trim() +
"') "
;
042.
043.
command.ExecuteNonQuery();
044.
MessageBox.Show(
"DATA SAVED"
);
045.
connection.Close();
046.
Cleardata();
047.
Showdata();
048.
}
049.
catch
(Exception ex)
050.
{
051.
MessageBox.Show(
"เกิดข้อผิดพลาดบางอย่าง"
+ex);
052.
053.
}
054.
}
055.
056.
private
void
btnEdit_Click(
object
sender, EventArgs e)
057.
{
058.
try
059.
{
060.
061.
connection.Open();
062.
OleDbCommand command =
new
OleDbCommand();
063.
command.Connection = connection;
064.
string
query =
"update Student set Title='"
+ textBox1.Text +
"', Fname='"
+ textBox2.Text +
"',Lname='"
+ textBox3.Text +
"',Bev='"
+ textBox1.Text +
"' where ID="
+ textBox6.Text +
""
;
065.
command.CommandText = query;
066.
067.
command.ExecuteNonQuery();
068.
MessageBox.Show(
"DATA Edit Success"
);
069.
connection.Close();
070.
Cleardata();
071.
Showdata();
072.
073.
}
074.
catch
075.
{
076.
MessageBox.Show(
"กรุณากลับไปเลือกและแก้ไขข้อมูลก่อนกดปุ่มแก้ไข"
);
077.
078.
}
079.
}
080.
081.
private
void
btnDelete_Click(
object
sender, EventArgs e)
082.
{
083.
try
084.
{
085.
connection.Open();
086.
OleDbCommand command =
new
OleDbCommand();
087.
command.Connection = connection;
088.
string
query =
"delete from Student where ID="
+ textBox6.Text +
""
;
089.
command.CommandText = query;
090.
091.
command.ExecuteNonQuery();
092.
MessageBox.Show(
"DATA Delete Success"
);
093.
connection.Close();
094.
Cleardata();
095.
Showdata();
096.
}
097.
catch
098.
{
099.
MessageBox.Show(
"กรุณากลับไปเลือกข้อมูลก่อนกดปุ่มลบ"
);
100.
101.
}
102.
}
103.
104.
private
void
btnLoad_Click(
object
sender, EventArgs e)
105.
{
106.
try
107.
{
108.
connection.Open();
109.
OleDbCommand command =
new
OleDbCommand();
110.
command.Connection = connection;
111.
string
query =
"select * from Student "
;
112.
command.CommandText = query;
113.
114.
OleDbDataAdapter da =
new
OleDbDataAdapter(command);
115.
DataTable dt =
new
DataTable();
116.
da.Fill(dt);
117.
dataGridView1.DataSource = dt;
118.
dataGridView1.Columns[0].HeaderText =
"รหัส"
;
119.
dataGridView1.Columns[0].Width = 50;
120.
dataGridView1.Columns[1].HeaderText =
"ชื่อ"
;
121.
dataGridView1.Columns[1].Width = 200;
122.
dataGridView1.Columns[2].HeaderText =
"นามสกุล"
;
123.
dataGridView1.Columns[2].Width = 200;
124.
dataGridView1.Columns[3].HeaderText =
"พฤติกรรม"
;
125.
dataGridView1.Columns[3].Width = 300;
126.
dataGridView1.Columns[4].HeaderText =
"วันที่ "
;
127.
dataGridView1.Columns[4].Width = 100;
128.
129.
connection.Close();
130.
131.
}
132.
catch
(Exception ex)
133.
{
134.
MessageBox.Show(
"Eror"
+ ex);
135.
136.
}
137.
}
138.
139.
private
void
dataGridView1_CellMouseClick(
object
sender, DataGridViewCellMouseEventArgs e)
140.
{
141.
if
(e.RowIndex>=0)
142.
{
143.
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
144.
textBox1.Text = row.Cells[1].Value.ToString();
145.
textBox2.Text = row.Cells[2].Value.ToString();
146.
textBox3.Text = row.Cells[3].Value.ToString();
147.
textBox4.Text = row.Cells[4].Value.ToString();
148.
textBox6.Text = row.Cells[0].Value.ToString();
149.
}
150.
}
151.
152.
private
void
btnSearch_Click(
object
sender, EventArgs e)
153.
{
154.
try
155.
{
156.
connection.Open();
157.
OleDbCommand command =
new
OleDbCommand();
158.
command.Connection = connection;
159.
string
query =
"SELECT * FROM Student WHERE Title LIKE '%"
+ txtSearch.Text +
"%'"
;
160.
command.CommandText = query;
161.
162.
DataTable dt =
new
DataTable();
163.
OleDbDataAdapter da =
new
OleDbDataAdapter(command);
164.
165.
da.Fill(dt);
166.
dataGridView1.DataSource = dt;
167.
dataGridView1.Columns[0].HeaderText =
"รหัส"
;
168.
dataGridView1.Columns[0].Width = 50;
169.
dataGridView1.Columns[1].HeaderText =
"ชื่อ"
;
170.
dataGridView1.Columns[1].Width = 200;
171.
dataGridView1.Columns[2].HeaderText =
"นามสกุล"
;
172.
dataGridView1.Columns[2].Width = 200;
173.
dataGridView1.Columns[3].HeaderText =
"พฤติกรรม"
;
174.
dataGridView1.Columns[3].Width = 300;
175.
dataGridView1.Columns[4].HeaderText =
"วันที่ "
;
176.
dataGridView1.Columns[4].Width = 100;
177.
178.
connection.Close();
179.
180.
}
181.
catch
(Exception ex)
182.
{
183.
MessageBox.Show(
"Eror"
+ ex);
184.
185.
}
186.
}
187.
188.
private
void
printDocument1_PrintPage(
object
sender, System.Drawing.Printing.PrintPageEventArgs e)
189.
{
190.
Bitmap bm =
new
Bitmap(
this
.dataGridView1.Width,
this
.dataGridView1.Height);
191.
dataGridView1.DrawToBitmap(bm,
new
Rectangle(0, 0,
this
.dataGridView1.Width,
this
.dataGridView1.Height));
192.
e.Graphics.DrawImage(bm, 0, 0);
193.
}
194.
195.
private
void
btnPrint_Click(
object
sender, EventArgs e)
196.
{
197.
PrintDialog printDialog =
new
PrintDialog();
198.
printDialog.Document = printDocument1;
199.
printDialog.UseEXDialog =
true
;
200.
201.
if
(DialogResult.OK == printDialog.ShowDialog())
202.
{
203.
printDocument1.DocumentName =
"Test Page Print"
;
204.
printDocument1.Print();
205.
}
206.
}
207.
208.
private
void
btnExit_Click(
object
sender, EventArgs e)
209.
{
210.
if
(MessageBox.Show(
"ต้องการปิดโปรแกรม ?"
,
""
, MessageBoxButtons.YesNo) == DialogResult.Yes)
211.
{
212.
Application.Exit();
213.
}
214.
}
215.
216.
private
void
Cleardata()
217.
{
218.
textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); txtSearch.Clear(); textBox6.Clear();
219.
}
220.
221.
222.
private
void
Showdata()
223.
{
224.
try
225.
{
226.
connection.Open();
227.
OleDbCommand command =
new
OleDbCommand();
228.
command.Connection = connection;
229.
string
query =
"select * from Student "
;
230.
command.CommandText = query;
231.
232.
OleDbDataAdapter da =
new
OleDbDataAdapter(command);
233.
DataTable dt =
new
DataTable();
234.
da.Fill(dt);
235.
dataGridView1.DataSource = dt;
236.
dataGridView1.Columns[0].HeaderText =
"รหัส"
;
237.
dataGridView1.Columns[0].Width = 50;
238.
dataGridView1.Columns[1].HeaderText =
"ชื่อ"
;
239.
dataGridView1.Columns[1].Width = 200;
240.
dataGridView1.Columns[2].HeaderText =
"นามสกุล"
;
241.
dataGridView1.Columns[2].Width = 200;
242.
dataGridView1.Columns[3].HeaderText =
"พฤติกรรม"
;
243.
dataGridView1.Columns[3].Width = 300;
244.
dataGridView1.Columns[4].HeaderText =
"วันที่ "
;
245.
dataGridView1.Columns[4].Width = 100;
246.
247.
connection.Close();
248.
249.
}
250.
catch
(Exception ex)
251.
{
252.
MessageBox.Show(
"Eror"
+ ex);
253.
254.
}
255.
}
256.
257.
}
258.
}