Database6_FinalExam
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
private OleDbConnection connection = new OleDbConnection();
OleDbDataAdapter dAdapter;
OleDbCommandBuilder cBuilder;
DataTable dTable = new DataTable();
BindingSource bSource;
private string ID;
public Form1()
{
InitializeComponent();
connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\student\Desktop\WindowsFormsApplication7\WindowsFormsApplication7\WindowsFormsApplication7\bin\Debug\test1.mdb");
}
private void Form1_Load(object sender, EventArgs e)
{
dAdapter = new OleDbDataAdapter("select * from person where 識別碼 ", connection);
cBuilder = new OleDbCommandBuilder(dAdapter);
dAdapter.Fill(dTable);
bSource = new BindingSource();
bSource.DataSource = dTable;
dataGridView1.DataSource = bSource;
}
private void button1_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection;
command2.CommandText = "insert into person (name,tel,stu_no,sex) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
command2.ExecuteNonQuery();
dAdapter.Fill(dTable);
connection.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
try
{
var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
Value = dataGridView1.Rows[e.RowIndex].Cells["name"].Value;
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select* from person where name='" + Value.ToString() + "'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
ID = reader["識別碼"].ToString();
textBox1.Text = reader["name"].ToString();
textBox2.Text = reader["tel"].ToString();
textBox3.Text = reader["stu_no"].ToString();
textBox4.Text = reader["sex"].ToString();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("ERROR" + ex);
}
}
else if (e.ColumnIndex == 1)
{
if (MessageBox.Show("確定刪除此筆資料?", "刪除資料", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{
var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
Value = dataGridView1.Rows[e.RowIndex].Cells["name"].Value;
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "delete from person WHERE name = '" + Value.ToString() + "'";
command.ExecuteNonQuery();
dTable.Clear();
dAdapter.Fill(dTable);
connection.Close();
MessageBox.Show("刪除成功");
}
catch (Exception ex)
{
MessageBox.Show("ERROR" + ex);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
dTable.Clear();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "UPDATE person SET name = '" + textBox1.Text + "'WHERE 識別碼 = " + ID ;
command.ExecuteNonQuery();
dAdapter.Fill(dTable);
}
}
}