您的位置:首页 > 数据库

C#与ACCESS数据库的链接和操作典型代码

2012-05-22 14:21 281 查看
using system;

using system.data.oledb;

class oledbtest{

public static void main()

{

/创建数据库连接

oledbconnection aconnection = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=c:\\db1.mdb");

/创建command对象并保存sql查询语句

oledbcommand acommand = new oledbcommand("select * from emp_test", aconnection);

try

{

aconnection.open();

/创建datareader 对象来连接到表单

oledbdatareader areader = acommand.executereader();

console.writeline("this is the returned data from emp_test table");

/关闭reader对象

areader.close();

/关闭连接,这很重要

aconnection.close();

}

/一些通常的异常处理

catch(oledbexception e)

{

console.writeline("error: {0}", e.errors[0].message);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: