您的位置:首页 > 数据库

C# 操作ACCESS数据库

2010-01-10 01:59 453 查看
原文:/article/5080428.html

在BETA2中,。NET提供了以下的NAMESPACE:
System.Data Namespace
System.Data.OleDb (和BETA1中已经不同了,所以如果拿BETA1中的程序到BETA2中来运行肯定不可以的)
如果想讲清楚这些东西,我不认为是我可以作到的,所以我想通过一些具体的程序来把我们对数据库的最基本的操作(SELECT、UPDATE、DELETE、INSERT等)演示一下,其他的还是需要朋友们在学习过程中来慢慢体会了!
要想操作一个数据库,不论是那种操作,首先要做的肯定是打开数据库,下面我们以ACCESS数据库来做例子说明如何打开一个数据库连接!在这里我们需要用到的是:System.Data.OleDb.OleDbConnection类!(如果操作SQL数据库,我们最好使用System.Data.SqlClient.SqlConnection类)
我先写出我自己使用的程序:
using System.Data
using System.Data.OleDb
public OleDbConnection getConn()
{
string connstr="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=F:\\web\\notesbook\\class\\leavenotes.mdb";
OleDbConnection tempconn= new OleDbConnection(connstr);
return(tempconn);
}
相信只要使用过ADO的朋友应该都可以看懂的!我们先定义一个String类型的变量,其中存放了我们连接数据库的连接字符串,然后在定义一个System.Data.OleDb.OleDbConnection类型的对象并实例化,最后返回这个对象!需要说明一下的是,我并没有把语句:tempconn.Open();放到这个函数中,原因我我稍后在说明,这里只是先提醒一下!
通过上面的函数,我们就已经得到了类似于ADO中的连接对象Connection了!下面的就是具体操作数据库了!
在具体讲操作前,我认为有必要先认识一下下面的两个类:
System.Data.OleDb.OleDbDataAdapter
System.Data.OleDb.OleDbDataReader
System.Data.OleDb.OleDbDataAdapter:可以直接和DataSet联系,并操作数据源的,它的功能相对强大一些,因此也比较耗系统资源!
System.Data.OleDb.OleDbDataReader:则有些类似于ADO中的哪个只读向前的记录集,它最常用在只需要依次读取并显示数据的时候,相比System.Data.OleDb.OleDbDataAdapter来说,他耗用的系统资源要小!其实,OleDbDataReader能实现的功能,OleDbDataAdapter都可以实现,不过从资源使用率的角度考虑我们应该尽量使用前者!但有些功能,却是必须使用OleDbDataAdapter才可以实现的!
* SELECT操作!
下面是我的自己在写测试程序的时候用到了,先列出来看看OleDbDataReader和OleDbDataAdapter是如何操作从数据库中选择记录的:
//通过ID得到当前留言详细内容.通过STRING类型参数
public Notebook getNoteFromID(string noteid)
{
Notebook tempnote=new Notebook(); //定义返回值
try
{
OleDbConnection conn = getConn(); //getConn():得到连接对象
string strCom = "Select * from notes where id=" + noteid ;
OleDbCommand myCommand =new OleDbCommand(strCom,conn);
conn.Open();
OleDbDataReader reader;
reader =myCommand.ExecuteReader() ; //执行command并得到相应的DataReader
//下面把得到的值赋给tempnote对象
if(reader.Read())
{
tempnote.id=(int)reader["id"];
tempnote.title=reader["title"].ToString();
tempnote.content=reader["content"].ToString();
tempnote.author=reader["author"].ToString();
tempnote.email=reader["email"].ToString();
tempnote.http=reader["http"].ToString();
tempnote.pic=reader["pic"].ToString();
tempnote.hits=(int)reader["hits"];
tempnote.posttime=(DateTime)reader["posttime"];
}
else //如没有该记录,则抛出一个错误!
{
throw(new Exception("当前没有该记录!"));
}
reader.Close();
conn.Close();
}
catch(Exception e)
{
//throw(new Exception("数据库出错:" + e.Message)) ;
}
return(tempnote); //返回Databook对象
}
上面的程序就是通过OleDbDataReader来得到特定的记录的!其中用到的语句我单独写到下面:
OleDbConnection conn = getConn(); //getConn():得到连接对象
string strCom = "Select * from notes where id=" + noteid ; //SQL语句
OleDbCommand myCommand =new OleDbCommand(strCom,conn); //建立OleDbCommand对象
conn.Open(); //注意我在前面说的Open语句在这里使用到了!
OleDbDataReader reader;
reader =myCommand.ExecuteReader() ; //执行command并得到相应的结果
我在每句话后都加入了说明,其中OleDbConnection conn = getConn();就是通过我前面提到的getConn函数来得到数据库连接的,其他语句没有什么好说的,都很简单,就不多说了!
我再列一个通过OleDbDataAdapter来得到记录的例程:
//Getlist():得到当前需要的留言列表
public DataView getNoteList()
{
DataView dataview;
System.Data.DataSet mydataset; //定义DataSet
try
{
OleDbConnection conn = getConn(); //getConn():得到连接对象
OleDbDataAdapter adapter = new OleDbDataAdapter();
string sqlstr="select * from notes order by posttime desc";
mydataset= new System.Data.DataSet();
adapter.SelectCommand = new OleDbCommand(sqlstr, conn);
adapter.Fill(mydataset,"notes");
conn.Close();
}
catch(Exception e)
{
throw(new Exception("数据库出错:" + e.Message)) ;
}
dataview = new DataView(mydataset.Tables["notes"]);
return(dataview);
}
这个程序或许有些复杂,同样的,我还是先把那些关键语句列出,并说明:
OleDbConnection conn = getConn(); //通过函数getConn()得到连接对象
OleDbDataAdapter adapter = new OleDbDataAdapter(); //实例化OleDbDataAdapter对象
string sqlstr="select * from notes order by posttime desc"; //SQL语句
mydataset= new System.Data.DataSet(); //由于OleDbDataAdapter需要和DataSet结合使用,所以在这里定义了DataSet对象,其实说OleDbDataAdapter复杂,其实就是因为DataSet的缘故DataSet有些类似于ADO中的recordset 对象,但功能远远超过了它,而且它和数据库是断开的,并能存放多个记录集!
adapter.SelectCommand = new OleDbCommand(sqlstr, conn); //设置命令为SelectCommand类型的
adapter.Fill(mydataset,"notes"); //执行,并将结果添加到mydataset中的”notes”表中
conn.Close(); //关闭连接!
在对上面的程序加一些补充说明,由于getNoteLista是得到一系列记录,并通过控件DataGrid来做分页显示的,所以我返回的是一个DataView类型的对象!
----------------------------------------
上次说了如何在ADO。NET中执行“SELECT”语句,这次我们看看,如何执行“DELETE、UPDATE、INSERT”等语句。
我们这次同样通过例子来看,其中我们用到了System.Data.OleDb.OleDbCommand类,其实,我们在前面执行SELECT的时候也用到了!
下面我写出我的程序:
//修改留言本中特定的数据
public Boolean UpdateNote(Notebook note)
{
Boolean tempvalue=false;
string sqlstr=""; //当时在这里定义,是为了在出现异常的时候看看我的SQL语句是否正确
try
{
//用到了我前面写的那个得到数据库连接的函数
OleDbConnection conn = getConn(); //getConn():得到连接对象,
conn.Open();
//确定我们需要执行的SQL语句,本处是UPDATE语句!
sqlstr = "UPDATE notes SET ";
sqlstr += "title='" + note.title + "',";
sqlstr += "content='" + DealString(note.content) +"',";
sqlstr += "author='" + note.author + "',";
sqlstr += "email='" +note.email +"',";
sqlstr += "http='" +note.http +"'";
//sqlstr += "pic='" +note.pic +"'";
sqlstr += " where id=" + note.id;
//定义command对象,并执行相应的SQL语句
OleDbCommand myCommand = new OleDbCommand(sqlstr,conn);
myCommand.ExecuteNonQuery(); //执行SELECT的时候我们是用的ExecuteReader()
conn.Close();
//假如执行成功,则,返回TRUE,否则,返回FALSE
tempvalue=true;
return(tempvalue);
}
catch(Exception e)
{
throw(new Exception("数据库更新出错:" + sqlstr + "\r" + e.Message)) ;
}
}
这个例子是对于特定ID好的记录进行UPDATE操作,具体解释我都写在了程序中,其中的与数据库有关的语句是try内部的那些!
其实,我们同样可以通过上面的那种模式执行INSERT、DELETE操作,下面我把我的程序列到下面!
/*删除特定记录,通过string类型的ID删除字段,在我的程序中,我把这个函数重载了,这样我们就可以通过INT类型的ID参数来删除特定的字段了*/
public Boolean DelNote(string delid)
{
Boolean tempvalue=false;
string sqlstr="";
//连接数据库
try
{
OleDbConnection conn = getConn(); //getConn():得到连接对象
conn.Open();
sqlstr = "delete * from notes where id=" + delid;
//定义command对象,并执行相应的SQL语句
OleDbCommand myCommand = new OleDbCommand(sqlstr,conn);
myCommand.ExecuteNonQuery();
conn.Close();
//假如执行成功,则,返回TRUE,否则,返回FALSE
tempvalue=true;
return(tempvalue);
}
catch(Exception e)
{
throw(new Exception("数据库更新出错:" + sqlstr + "\r" + e.Message)) ;
}
}
细心的朋友们应该能看到,其实这个程序和上面的相比,只是哪个SQL语句不同而已,其他的都基本一样的!同样的,我们想在数据库中插入新的记录的时候也可以用这样的方式,程序如下:
//向留言本中添加数据
public Boolean AddNote(Notebook note)
{
Boolean tempvalue=false; //定义返回值,并设置初值
//下面把note中的数据添加到数据库中!
try{
OleDbConnection conn = getConn(); //getConn():得到连接对象
conn.Open();
//设置SQL语句
string insertstr="INSERT INTO notes(title, content, author, email, http, pic ,hits,posttime) VALUES ('";
insertstr += note.title +"', '";
insertstr += DealString(note.content) + "','";
insertstr += note.author + "','";
insertstr += note.email + "','";
insertstr += note.http + "','";
insertstr += note.pic + "',";
insertstr += note.hit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: