本文主要是介绍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 '绑定对象的定义,名为mybindPrivate Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load '系统自动生成的Dim strconn As String = "Server=localhost;Database=学生成绩管理系统;Integrated Security=SSPI" '建立一个连接字符串strconnDim strsql As String = "select Snum,Sname,Ssex,Sbirth,Sdept,province from Student" '建立一个查询字符串strsqlDim myconnect As SqlConnection = New SqlConnection(strconn) '建立一个数据连接myconnectDim 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.Clickmybind.Position = 0 '跳转到第一条End Sub
双击"上一条"按钮
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickIf mybind.Position > 0 Thenmybind.Position -= 1End IfEnd Sub
双击"下一条"按钮
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickIf mybind.Position > 0 Thenmybind.Position += 1End IfEnd Sub
双击"最后一条"按钮
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Clickmybind.Position = mybind.Count - 1End Sub
这篇关于VS2019使用VB/ADO.Net组件设计一个窗体连接SQL server数据库中学生表的内容并显示,实现数据绑定,表格内容前后查看的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!