栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

先看一下效果图:文本框用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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910300.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号