[我的ASP.net学习历程]DataBase Settion(3)

类别:Asp 点击:0 评论:0 推荐:
  protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
Random rd = new Random();
string dbPath = @"Data\dbTest.mdb";
string db = Server.MapPath (dbPath);
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db;
string strSQL = "SELECT [CustomerID],[Address] FROM Customers";

DataSet ds = new DataSet();

OleDbConnection conn = new OleDbConnection(connectionString);
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

//通过Connection链接数据库操作,参数1为以第一行为起,参数2为向下两行止
//da.Fill(ds,1,2,"testMdb");
da.Fill(ds,"testMdb");
//向DataSet插入一新行
DataRow dr = ds.Tables["testMdb"].NewRow();

dr["CustomerID"] = "RandNumber";
dr["Address"] = rd.Next(100000).ToString();
ds.Tables["testMdb"].Rows.Add(dr); //填充内容到DataSet

da.Update(ds,"testMdb");

DataGrid1.DataSource = ds.Tables["testMdb"].DefaultView;
DataGrid1.DataBind();

ds.Clear();
conn.Close();

}

本文地址:http://com.8s8s.com/it/it8531.htm