当前位置:文档之家› ASP实验报告四

ASP实验报告四

ASP实验报告四
ASP实验报告四

A S P实验报告四

标准化管理处编码[BBX968T-XBB8968-NNJ668-MM9N]

实验报告四实验题目:数据源控件与数据绑定控件

指导老师:马浩

专业班级:计算机科学与技术系1105班

姓名:王媛(29)

2014年5月24日

一、实验题目

数据源的控件与数据绑定控件

二、实验目的

1、掌握在SQL Server 2005中创建数据的创建、备份。.

2、掌握数据源控件sqldatasource的设置和使用。

3、掌握常用数据绑定控件gridview、formview、listview的使用。

4、掌握以上控件实现对数据库的增删改查。

三、实验代码及截图

实验代码:

1、

ports

Imports Class_1101_gridviewtest

Inherits Dim conn As SqlConnection

Dim connstr As String = "Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False"

Protected Sub Page_Load(ByVal sender As Object, ByVal e As Handles If Not IsPostBack Then

ViewState("sortorder") = "id"

ViewState("orderdire") = "asc"

bind()

End If

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As Handles ("")

End Sub

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As Handles

'Dim conn As New SqlConnection()

conn = New SqlConnection(connstr)

Dim cmd As New SqlCommand()

' = "Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False"

Dim sqlstr = "delete from student where id='" & .() & "'" ()

= conn

= sqlstr

()

()

bind()

End Sub

Public Sub bind()

' Dim conn As New SqlConnection()

conn = New SqlConnection(connstr)

Dim sqlstr As String = "select * from student"

' = "Data Source=.;Initial Catalog=aspnet;Integrated

Security=SSPI;Pooling=False"

Dim ds As New DataSet()

()

Dim objadapter As New SqlDataAdapter(sqlstr, conn)

(ds, "student")

Dim view As DataView = ("student").DefaultView

Dim sort As String = ViewState("sortorder").ToString & " " & ViewState("orderdire").ToString

= sort

= view

()

()

End Sub

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As Handles

=

bind()

Dim drop_str As String = CType.FindControl("HiddenField1"), HiddenField).

CType.FindControl("DropDownList1"), DropDownList).Text = drop_str End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As Handles

Dim xuehao As TextBox = .Cells(1).Controls(0)

Dim name As TextBox = .Cells(2).Controls(0)

Dim addr As String = CType.Cells(3).Controls(1), DropDownList).

Dim tel As TextBox = .Cells(4).Controls(0)

Dim sqlstr As String = "update student set xuehao='" & & "',name='" & & "',addr='" & () & "',tel='" & & "' where id='" & .() & "'"

' Dim conn As New SqlConnection()

conn = New SqlConnection(connstr)

Dim cmd As New SqlCommand()

' = "Data Source=.;Initial Catalog=aspnet;Integrated

Security=SSPI;Pooling=False"

'Dim sqlstr = "delete from student where id='" & .() & "'"

()

= conn

= sqlstr

()

()

= -1

bind()

End Sub

Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As Handles

= -1

bind()

End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As Handles

=

End Sub

Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As Handles

bind()

End Sub

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As Handles

Dim spage As String =

If ViewState("sortorder") = spage Then

If ViewState("orderdire") = "asc"Then

ViewState("orderdire") = "desc"

Else

ViewState("orderdire") = "asc"

End If

Else

ViewState("sortorder") =

End If

bind()

End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As Handles

'If = Then

' If = Or = Then

' 'CType LinkButton).("onclick", "javascript:return confirm('你确认删除" & & "吗')")

' End If

'End If

Dim i As Integer

For i = 0 To If = Then

"onmouseover", "c='#00A9FF'")

"onmouseout", "")

End If

Next i

End Sub

Protected Sub quanxuan_CheckedChanged(ByVal sender As Object, ByVal e As Handles

Dim i As Integer

For i = 0 To - 1

Dim cbox As CheckBox = (i).FindControl("CheckBox1")

If Then

= True

Else

= False

End If

Next

End Sub

Protected Sub delete_Click(ByVal sender As Object, ByVal e As Handles conn = New SqlConnection(connstr)

Dim cmd As SqlCommand

Dim i As Integer

For i = 0 To - 1

Dim cbox As CheckBox = (i).FindControl("CheckBox1")

If Then

Dim sqlstr As String = "delete from student where id='" & (i). & "'"

cmd = New SqlCommand(sqlstr, conn)

()

()

()

End If

Next

bind()

End Sub

Protected Sub cancel_Click(ByVal sender As Object, ByVal e As Handles

= False

Dim i As Integer

For i = 0 To - 1

Dim cbox As CheckBox = (i).FindControl("CheckBox1")

= False

Next

End Sub

Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As Handles

("id='" & . & "'")

End Sub

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As Handles

End Sub

End Class

2、

Imports

Imports Class_1101_adduser

Inherits Protected Sub Button1_Click(ByVal sender As Object, ByVal e As Handles

Try

Dim conn As New SqlConnection()

Dim cmd As New SqlCommand()

= "Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False"

Dim sqlstr = "insert into student(id,xuehao,name,addr,tel) values('" & & "','" & & "','" & & "','" & & "','" & & "')"

()

= conn

= sqlstr

()

()

("")

Catch ex As Exception

("用户为主键,不能重复出现")

End Try

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As Handles

End Sub

End Class

3、

Imports

Imports Class_1101_xiangxi

Inherits Protected Sub Page_Load(ByVal sender As Object, ByVal e As Handles

Try

Dim conn As New SqlConnection()

Dim cmd As New SqlCommand()

Dim rend As SqlDataReader

= "Data Source=.;Initial Catalog=aspnet;Integrated

Security=SSPI;Pooling=False"

Dim sqlstr = "select * from student where id=" & Request("id") ()

= conn

= sqlstr

rend = ()

()

= "学号: & ("xuehao") & "
"

+= "姓名: & ("name") & "
"

+= "家庭地址:& ("addr") & "
"

+= "电话:& ("tel") & "
"

()

Catch ex As Exception

("用户信息查询败")

End Try

End Sub

End Class

截图:

五、实验总结

通过本次实验,我掌握了在SQL Server 2005中创建数据的创建、备份;.

掌握了数据源控件sqldatasource的设置和使用;掌握了常用数据绑定控件gridview、formview、listview的使用;以及以上控件实现对数据库的增删改查。

相关主题
文本预览
相关文档 最新文档