DataSet中的操作更新到Access数据库

时间:2010-03-10 12:41:49  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:bsp; Sub page_load()sub page_load()    dim strConnection as string    dim strSQL as string    dim objDataSet as new DataSet()    dim objConnection as OledbConnection    dim objAdapter as OledbDataAdapter       strConnection = "Provider=Mi...
  正文:代码如下:
  
  <%@import Namespace =Namespace=system.data%>
  <%@import Namespace =Namespace=system.data.oledb%>
  <script language=vb runat=server>
  Sub page_load()sub page_load()
   dim strConnection as string
   dim strSQL as string
   dim objDataSet as new DataSet()
   dim objConnection as OledbConnection
   dim objAdapter as OledbDataAdapter
  
   strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=h:BegASP/ target=_blank class=infotextkey>aspNETNorthwind.mdb" '使用的数据库是Access自带的示例文件:Northwind.mdb
   strSQL = "select 雇员ID,姓氏,名字 from 雇员"
  
   objConnection = new OledbConnection(strConnection)
   objAdapter = new OledbDataAdapter(strSQL , objConnection)
  
   objAdapter.fill(objDataSet , "Employees")
  
   dgNameList1.DataSource = objDataSet.Tables("Employees").DefaultView
   dgNameList1.DataBind()
  
   '-----------------------------------------------------------向表添加一个新行
   dim objTable as DataTable
   dim objNewRow as DataRow
  
   objTable = objDataSet.Tables("Employees")
   objNewRow = objTable.NewRow()
   objNewRow("姓氏") = "聂"
   objNewRow("名字") = "大鹏"
   objTable.Rows.Add(objNewRow)
  
   objNewRow = objTable.NewRow()
   objNewRow("姓氏") = "王"
   objNewRow("名字") = "志东"
   objTable.Rows.Add(objNewRow)
  
   dgNameList2.DataSource = objTable.DefaultView
   dgNameList2.DataBind()
  
  
   '-----------------------------------------------------------更新表中的行
   dim objRow as DataRow
  
   objRow = objTable.Rows(3)
   objRow("姓氏") = "谷"
   objRow("名字") = "丰"
  
   dgNameList3.DataSource = objTable.DefaultView
   dgNameList3.DataBind()
  
  
  
   '-----------------------------------------------------------删除表中的行
   objTable.Rows(objTable.Rows.Count - 2).Delete()
  
   dgNameList4.DataSource = objTable.DefaultView
   dgNameList4.DataBind()
  
  
  
   '================================================
更新数据源
   dim objBuilder as OledbCommandBuilder
  
   objBuilder = new OledbCommandBuilder(objAdapter)
   objAdapter.UpdateCommand = objBuilder.GetUpdateCommand()
   objAdapter.InsertCommand = objBuilder.GetInsertCommand()
   objAdapter.DeleteCommand = objBuilder.GetDeleteCommand()
  
   objAdapter.Update(objDataSet , "Employees")
  end sub
  </script>
  
  <html>
  <body>
  <table width=100%>
   <tr>
   <td>原始数据</td>
   <td>添加行</td>
   <td>编辑行</td>
   <td>删除行</td>
   </tr> 《DataSet中的操作更新到Access数据库》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/aspnet/13163.html


关键字:

关于《DataSet中的操作更新到Access数据库》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td