网络应用与开发2(2)
发布时间:2021-06-05
发布时间:2021-06-05
'建立DataAdapter对象,并且指定SQL语句
Dim myda As New OleDbDataAdapter (mysql, objconn) ‘第三空 Dim myds As New DataSet() ‘第四空
(myds, "stu_info") ‘第五空 Response.Write("数据集对象dataset已被填充") End If
End Sub
5、通过DataAdapter对象获取数据库Sample.mdb中表Stu_info,Teacher_info,Coerse_info中的记录,并填充到DataSet对象中 Imports System.Data
Imports System.Data.OleDb
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码
If Not Page.IsPostBack Then ‘第一空 Dim objconn As New OleDbConnection '设置链接字符串
objconn.ConnectionString =
source=" & Server.MapPath("sample.mdb") "Provider=Microsoft.JET.OLEDB.4.0;Data
‘第二空
objconn.Open()
Dim mysql As String
mysql = "select * from stu_info" '建立第一张表
'建立DataAdapter对象,并且指定SQL语句
Dim myda As New OleDbDataAdapter(mysql, objconn) Dim myds As New DataSet
myda.Fill(myds, "学生信息表") '建立第二张表
mysql = "select * from teacher_info"
myda = New oleDbDataAdapter (mysql, objconn) ‘第三空 myda.Fill(myds, "教师信息表") '建立第三张表
mysql = "select * from course_info"
myda = New OleDbDataAdapter(mysql, objconn)
myda. Fill (myds, "课程信息表") ‘第四空 '输出三个表的表名
Dim i As Integer
For i = 0 To myds.Tables.Count-1 ‘第五空 Response.Write(myds.Tables(i).TableName) Response.Write("<p>") Next
objconn.Close() End If End Sub 6、用DataView对象象作为数据列表控件(DataGrid对象)的数据源,显示Sample.mdb数据库中Stu_info表的信息。
操作如下:添加Sample.mdb到项目中,插入DataGrid控件,属性均为默认,代码如下:
Imports System.Data
Imports System.Data.OleDb
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then
Dim objconn As New OleDbConnection
objconn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data source=" & Server.MapPath("sample.mdb") Dim mysql As String
mysql = "select * from stu_info"
Dim myda As New oleDbDataAdapter (mysql, objconn) ‘第一空 '声明一个DataSet
Dim myds As New DataSet ‘第二空 '用DataAdapter的FILL方法填充DataSet myda.Fill(myds, "学生信息表") '定义DataView对象 Dim mydv As DataView
mydv = myds.Tables("学生信息表"). DefaultView ‘第三空 DataGrid1. DataSource = mydv ‘第四空 DataGrid1. DataBind() ‘第五空 End If End Sub
7、新闻管理系统的登录页面:用户名的ID为username,密码的ID为password。 Dim username, password As String
If Page Then ‘第一空 username = Request.Form("username") ‘第二空