网络应用与开发2
发布时间:2021-06-05
发布时间:2021-06-05
1、利用command对象的ExecuteScalar()方法读出Sample.mdb数据库中stu_info表的记录总数。
Imports System.Data
Imports ‘第一空
Private Sub insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles insert.Click
Dim objconn As New OleDbConnection ‘第二空
& Server.MapPath("sample.mdb") ‘第三空
‘第四空 Dim mySQL As String = "select count(*) from stu_info" Dim objCmd As New OleDbCommand(mySQL, objconn)
Dim totalrow As Integer = objCmd.ExecuteScalar() ‘第五空 Response.Write("记录总数为:" & totalrow) objconn.Close() End Sub
2、往sample.mdb数据库中stu_info表中插入一条记录,表中字段:学号、姓名、专业。
Imports System.Data
System.Data.OleDb ‘第一空
Private Sub insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles insert.Click
Dim objconn As New OleDbConnection ‘第二空
;Data source=" & Server.MapPath("sample.mdb") ‘第三空
objconn. ‘第四空 Dim mysql As String
mysql = "insert into stu_info(学号,姓名,专业) values('1001','王红','计算机应用')"
Dim objcmd As New OleDbCommand(mysql, objconn) ‘第五空 objconn.Close() Response.Write("一条记录插入成功") End Sub 3、用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
4、通过DataAdapter对象获取数据库Sample.mdb中表Stu_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.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"