<%@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:BegASPNETNorthwind.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>
<tr>
<td valign=top><asp:datagrid id=dgNameList1 runat=server /></td>
<td valign=top><asp:datagrid id=dgNameList2 runat=server /></td>
<td valign=top><asp:datagrid id=dgNameList3 runat=server /></td>
<td valign=top><asp:datagrid id=dgNameList4 runat=server /></td>
</tr>
</table>
<hr>
<asp:datagrid id=dgUpd runat=server />
</body>
</html>
<%@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:BegASPNETNorthwind.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>
<tr>
<td valign=top><asp:datagrid id=dgNameList1 runat=server /></td>
<td valign=top><asp:datagrid id=dgNameList2 runat=server /></td>
<td valign=top><asp:datagrid id=dgNameList3 runat=server /></td>
<td valign=top><asp:datagrid id=dgNameList4 runat=server /></td>
</tr>
</table>
<hr>
<asp:datagrid id=dgUpd runat=server />
</body>
</html>