net操作access数据库示例分享
using system;
using system.data;
using system.configuration;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.data.oledb;
/// <summary>
///accessdb 的摘要说明
/// </summary>
public class accessdb
{
public accessdb()
{
//todo: 在此处添加构造函数逻辑
}
/// <summary>
/// 读取access数据库
/// </summary>
private void readaccessdata()
{
//1、建立连接
string saccessconnection = @"provider=microsoft.jet.oledb.4.0;data source=c:\documents and settings\administrator.www-410adc78208\桌面\应用程序.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();
//查询并显示数据
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);
}
//关闭连接
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\桌面\应用程序.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' )";
//执行
odcommand.executenonquery();
//关闭连接
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\桌面\应用程序.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;
//执行
odcommand.executenonquery();
//关闭连接
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\桌面\应用程序.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;
//执行
odcommand.executenonquery();
//关闭连接
odcconnection.close();
this.listview1.items.clear();
readaccessdata();
}
}