/// summary>
///AccessDB 的摘要說明
/// /summary>
public class AccessDB
{
public AccessDB()
{
//TODO: 在此處添加構(gòu)造函數(shù)邏輯
}
/// summary>
/// 讀取Access數(shù)據(jù)庫
/// /summary>
private void ReadAccessData()
{
//1、建立連接
string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator.WWW-410ADC78208\桌面\應(yīng)用程序.mdb";
OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
//2、打開連接
odcConnection.Open();
//建立SQL查詢
OleDbCommand odCommand = odcConnection.CreateCommand();
//3、輸入查詢語句
odCommand.CommandText = "SELECT table.編號, table.[123], table.[456] FROM [table];";
//建立讀取
OleDbDataReader odrReader = odCommand.ExecuteReader();
//查詢并顯示數(shù)據(jù)
while (odrReader.Read())
{
ListViewItem item = new ListViewItem();
item.SubItems.Add(odrReader[0].ToString());
item.SubItems.Add(odrReader[1].ToString());
item.SubItems.Add(odrReader[2].ToString());
this.listView1.Items.Add(item);
}
//關(guān)閉連接
odrReader.Close();
odcConnection.Close();
}
/// summary>
/// add
/// /summary>
/// param name="sender">/param>
/// param name="e">/param>
private void button1_Click(object sender, EventArgs e)
{
//1、建立連接
string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator.WWW-410ADC78208\桌面\應(yīng)用程序.mdb";
OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
//2、打開連接
odcConnection.Open();
//建立SQL查詢
OleDbCommand odCommand = odcConnection.CreateCommand();
//3、輸入查詢語句
odCommand.CommandText = "INSERT INTO [table]([123],[456]) VALUES('88','88' )";
//執(zhí)行
odCommand.ExecuteNonQuery();
//關(guān)閉連接
odcConnection.Close();
this.listView1.Items.Clear();
ReadAccessData();
}
/// summary>
/// delete
/// /summary>
/// param name="sender">/param>
/// param name="e">/param>
private void button3_Click(object sender, EventArgs e)
{
//1、建立連接
string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator.WWW-410ADC78208\桌面\應(yīng)用程序.mdb";
OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
//2、打開連接
odcConnection.Open();
//建立SQL查詢
OleDbCommand odCommand = odcConnection.CreateCommand();
//3、輸入查詢語句
string sql = "DELETE table.編號 FROM [table] WHERE (((table.編號)=" + this.listView1.SelectedItems[0].SubItems[1].Text + "))";
odCommand.CommandText = sql;
//執(zhí)行
odCommand.ExecuteNonQuery();
//關(guān)閉連接
odcConnection.Close();
this.listView1.Items.Clear();
ReadAccessData();
}
/// summary>
/// update
/// /summary>
/// param name="sender">/param>
/// param name="e">/param>
private void button2_Click(object sender, EventArgs e)
{
//1、建立連接
string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator.WWW-410ADC78208\桌面\應(yīng)用程序.mdb";
OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
//2、打開連接
odcConnection.Open();
//建立SQL查詢
OleDbCommand odCommand = odcConnection.CreateCommand();
//3、輸入查詢語句
string sql = "UPDATE [table] SET 123='11',456='11' WHERE (((table.編號)=" + this.listView1.SelectedItems[0].SubItems[1].Text + "))";
odCommand.CommandText = sql;
//執(zhí)行
odCommand.ExecuteNonQuery();
//關(guān)閉連接
odcConnection.Close();
this.listView1.Items.Clear();
ReadAccessData();
}
}