先看一下效果图:文本框用label控件,按钮用button控件,显示用框TextBox控件,(Name)可以自己修改
建立一个学生表名字为Student
双击窗体进入代码界面,最顶上输入:
Imports System.Data.SqlClient '导入命名空间
窗体的代码:
Public Class UserControl1 '系统自动生成的
Dim ds As DataSet = New DataSet '建立类中可访问数据对象ds,且为全局变量
Public mybind As BindingManagerBase '绑定对象的定义,名为mybind
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load '系统自动生成的
Dim strconn As String = "Server=localhost;Database=学生成绩管理系统;Integrated Security=SSPI" '建立一个连接字符串strconn
Dim strsql As String = "select Snum,Sname,Ssex,Sbirth,Sdept,province from Student" '建立一个查询字符串strsql
Dim myconnect As SqlConnection = New SqlConnection(strconn) '建立一个数据连接myconnect
Dim mycommand As SqlDataAdapter = New SqlDataAdapter(strsql, myconnect) '建立一个数据适配器mycommand对数据执行sql指令
mycommand.Fill(ds, "Student")'指定绑定的数据源
mybind = Me.BindingContext(ds, "Student")
'TextBox控件的Text属性绑定至数据集ds内的Student表
TextBoxSnum.DataBindings.Add("Text", ds, "Student.Snum")
TextBoxSname.DataBindings.Add("Text", ds, "Student.Sname")
TextBoxSsex.DataBindings.Add("Text", ds, "Student.Ssex")
TextBoxSdept.DataBindings.Add("Text", ds, "Student.Sdept")
TextBoxSbirth.DataBindings.Add("Text", ds, "Student.Sbirth")
TextBoxprovince.DataBindings.Add("Text", ds, "Student.province")
End Sub
双击"第一条"按钮
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mybind.Position = 0 '跳转到第一条
End Sub
双击"上一条"按钮
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If mybind.Position > 0 Then
mybind.Position -= 1
End If
End Sub
双击"下一条"按钮
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If mybind.Position > 0 Then
mybind.Position += 1
End If
End Sub
双击"最后一条"按钮
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
mybind.Position = mybind.Count - 1
End Sub