您的位置:首页 > 数据库

vb.net 连接数据库及rs.MoveFirst() rs.MoveNext() rs.MoveLast() 用法

2011-08-01 15:11 661 查看
登陆框代码:

'登陆框
Public Class 登陆

Private Sub okbtn_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles okbtn.Click
If (Trim(usertext.Text) = "" Or Trim(passwordtext.Text) = "")

Then
MsgBox("用户名或密码不能为空", vbOKOnly, "提示") : Exit Sub
Else
Call connect()
End If
End Sub

Public Sub connect()
Dim pass As Boolean
pass = False
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim constr As String
Dim i As Integer
Dim sql As String
constr = "driver={SQL Server};server=PC-201105311336

\SQLEXPRESS;Uid=sa;Pwd=123456;Database=jiaxiaoai" '无源连接
'constr = "dsn=sqlserver;Uid=sa;Pwd=123456;Database=jiaxiaoai"

'有源连接
conn.Open(constr)
sql = "select * from sysname"
'rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenKeyset,

ADODB.LockTypeEnum.adLockReadOnly)
rs.Open(sql, conn, 1, 1)
If conn.State = 1 Then
For i = 1 To rs.RecordCount
If rs(0).Value = usertext.Text And rs(1).Value =

passwordtext.Text Then
pass = True
Else
If rs.EOF = True Then
Exit For
rs.MoveNext()
End If
End If
Next
If pass = True Then
Me.Hide()
显示学生信息.Show()'重点
Else
MsgBox("用户名或密码错误")
End If
Else
MsgBox("连接失败:state=" & conn.State)
End If

End Sub

Private Sub 登陆_Load(ByVal sender As Object, ByVal e As

System.EventArgs) Handles MyBase.Load
usertext.Text = ""
passwordtext.Text = ""
End Sub

Private Sub cancelbtn_Click(ByVal sender As System.Object, ByVal e

As System.EventArgs) Handles cancelbtn.Click
Close()

End Sub

End Class




学生信息提示框:

'学生信息显示框
Public Class 显示学生信息
Dim rs As New ADODB.Recordset '定义一个全局变量

Public Sub connect()
Dim conn As New ADODB.Connection
Dim constr As String
Dim sql As String
constr = "driver={SQL Server};server=PC-201105311336

\SQLEXPRESS;Uid=sa;Pwd=123456;Database=jiaxiaoai" '无源连接
conn.Open(constr)
sql = "select * from student"
rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenKeyset,

ADODB.LockTypeEnum.adLockReadOnly)
End Sub

Private Sub firstbtn_Click(ByVal sender As System.Object, ByVal e

As System.EventArgs) Handles firstbtn.Click
rs.MoveFirst()
xuehaotext.Text = rs(0).Value
nametext.Text = rs(1).Value
sextext.Text = Str(rs(2).Value)
scoretext.Text = Str(rs(3).Value)
End Sub

Private Sub upbtn_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles upbtn.Click
rs.MovePrevious()
If rs.BOF = True Then
rs.MoveFirst()
End If
xuehaotext.Text = rs(0).Value
nametext.Text = rs(1).Value
sextext.Text = Str(rs(2).Value)
scoretext.Text = Str(rs(3).Value)

End Sub

Private Sub downbtn_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles downbtn.Click
rs.MoveNext()
If rs.EOF = True Then
rs.MoveLast()
End If
xuehaotext.Text = rs(0).Value
nametext.Text = rs(1).Value
sextext.Text = Str(rs(2).Value)
scoretext.Text = Str(rs(3).Value)

End Sub

Private Sub endbtn_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles endbtn.Click
rs.MoveLast()
xuehaotext.Text = rs(0).Value
nametext.Text = rs(1).Value
sextext.Text = Str(rs(2).Value)
scoretext.Text = Str(rs(3).Value)

End Sub

Private Sub 显示学生信息_Load(ByVal sender As Object, ByVal e As

System.EventArgs) Handles MyBase.Load '窗口初始化函数(重点)
Call connect()
rs.MoveFirst()
xuehaotext.Text = rs(0).Value
nametext.Text = rs(1).Value
sextext.Text = Str(rs(2).Value)
scoretext.Text = Str(rs(3).Value)
End Sub
End Class


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: