連接本地Access數據庫及簡單操作的winform小程序
一、准備工作
用Access創建一個數據庫並創建一個表格。(對於非遠程數據庫,Access十分簡單。表格可參考三、界面設計)。
二、代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Common;
namespace MyFirstDatabase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection conn;
OleDbCommand da;
private void Form1_Load(object sender, EventArgs e)
{
string txtConn =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\JOKER\\Documents\\Database2.mdb";
conn = new OleDbConnection(txtConn);
string txtCommand = "SELECT StudentName, StudentNum, StudentSex FROM Student";
OleDbDataAdapter da = new OleDbDataAdapter(txtCommand, conn);
DataSet ds = new DataSet("ds");
da.Fill(ds, "Student");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Student";
//dataGridView2.DataSource = ds;
//dataGridView2.DataMember = "Student";
}
private void button1_Click(object sender, EventArgs e) //增加紀錄
{
if (textBox1.Text == "" ||textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("所有項都必須填寫! ");
return;
}//注意,下邊語句必須保證數據庫文件studentI.mdb在文件夾路徑C:\\**中
string txt1 =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\JOKER\\Documents\\Database2.mdb";
string txt2 =
"Insert Into Student(StudentNum,StudentName,StudentSex) Values('";
txt2 += textBox1.Text + "' , '";
txt2 += textBox2.Text + "' , '";
txt2 += textBox3.Text + "')";//最后結果要保證在TextBox中輸入的內容被單引號括住
conn = new OleDbConnection(txt1);
conn.Open();
da = new OleDbCommand();
da.CommandText = txt2;
da.Connection = conn;
da.ExecuteNonQuery();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
conn.Close();
}
private void button2_Click(object sender, EventArgs e) //修改紀錄
{
if (textBox1.Text == "" )
{
MessageBox.Show("學生編號項必須填寫! ");
return;
}//注意,下邊語句必須保證數據庫文件studentI.mdb在文件夾路徑C:\\**中
string txt1 =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\JOKER\\Documents\\Database2.mdb";
string txt2 = "Update Student Set StudentName = '" + textBox2.Text +"',StudentSex = '"+textBox3.Text+"'Where StudentNum = " + textBox1.Text;
conn = new OleDbConnection(txt1);
conn.Open();
da = new OleDbCommand();
da.CommandText = txt2;
da.Connection = conn;
da.ExecuteNonQuery();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
conn.Close();
}
private void button3_Click(object sender, EventArgs e) //更新紀錄
{
string txtConn =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\JOKER\\Documents\\Database2.mdb";
conn = new OleDbConnection(txtConn);
string txtCommand = "SELECT StudentName, StudentNum, StudentSex FROM Student";
OleDbDataAdapter da = new OleDbDataAdapter(txtCommand, conn);
DataSet ds = new DataSet("ds");
da.Fill(ds, "Student");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Student";
}
private void button4_Click(object sender, EventArgs e) //刪除紀錄
{
if (textBox1.Text == "")
{
MessageBox.Show("學生編號項必須填寫! ");
return;
}//注意,下邊語句必須保證數據庫文件studentI.mdb在文件夾路徑C:\\**中
string txt1 =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\JOKER\\Documents\\Database2.mdb";
string txt2 = "Delete From Student Where StudentNum = " + textBox1.Text;
conn = new OleDbConnection(txt1);
conn.Open();
da = new OleDbCommand();
da.CommandText = txt2;
da.Connection = conn;
da.ExecuteNonQuery();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
conn.Close();
}
private void button5_Click(object sender, EventArgs e) //篩選數據
{
string txtConn =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\JOKER\\Documents\\Database2.mdb";
conn = new OleDbConnection(txtConn);
string txtCommand = "SELECT "+comboBox1.Text+ ","+comboBox2.Text+" FROM Student";
OleDbDataAdapter da = new OleDbDataAdapter(txtCommand, conn);
DataSet ds = new DataSet("ds");
da.Fill(ds, "Student");
dataGridView2.DataSource = ds;
dataGridView2.DataMember = "Student";
}
}
}
三、界面設計
如下圖:

Written with StackEdit.
