VS2019使用VB/ADO.Net组件设计一个窗体连接SQL server数据库中学生表的内容并显示,实现数据绑定,表格内容前后查看

先看一下效果图:文本框用label控件,按钮用button控件,显示用框TextBox控件,(Name)可以自己修改
点击最底下的4个按钮可以查看学生表中的学生信息,第一条和最后一条查看第一个人和最后一个人的信息
建立一个学生表名字为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
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值