using System.Data;
using System.Data.OleDb;
{
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("DB.mdb");
conn.Open();
string str="Insert into stu values('3','顾惜朝','M')";
//string str="Delete from stu where ID='1'";
OleDbCommand cmd=new OleDbCommand(str,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
例子二:使用DataReader对象读取数据。
DataReader对象只能读取数据,不能写入数据,而且是将数据库从头到尾依序读出。当DataReader对象被创建时,指针会停留在数据的最前端,我们可以使用Read()方法一次读取一条记录。
using System.Data;
using System.Data.OleDb;
public partial class NewFolder5_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(); //创建数据连接
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("DB.mdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from stu";
OleDbDataReader reader =cmd.ExecuteReader();//创建DataReader对象
Response.Write("<Table Border='1'><tr align='center'>");
for (int i = 0; i < reader.FieldCount; i++) //读取并显示所有字段的名称
Response.Write("<td>"+reader.GetName(i)+"</td>");
Response.Write("</tr>");
while (reader.Read())
{
Response.Write("<tr>");
for(int j=0;j<reader.FieldCount;j++)
Response.Write("<td>" + reader.GetValue(j) + "</td>");//读取并显示所有字段的内容
Response.Write("</tr>");
}
Response.Write("</table>");
reader.Close(); //关闭DataReader()对象
conn.Close(); //关闭数据连接
}
}
protected void Page_Load(object sender, EventArgs e)