请问在vb.net中如何把sql中的数据读取到textbox中?

2024年11月16日 03:34
有1个网友回答
网友(1):

Imports System ' 引入数据库操作类命名空间
Imports System.Data
Imports System.Data.SqlClient
Public Class 登录
Dim i As Integer ' 声明计数器i,用来记录键亩登录次数

' 取消按钮事件,当单击取消按钮,清除输入信息
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub

' 计时器事件,控制登录时间,使用标签来模拟计时器显示
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label5.Text = Val(Label5.Text) - 1
If Val(Label5.Text) = 0 Then ' 当标签显示文本为0时
Timer1.Enabled = False ' 计时器不再使用
MessageBox.Show("登录时间到,退出该系统!", "系统纯宽提示", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Me.Close()
End If
End Sub

' 登录按钮事件,控制登录
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New 主窗体
Dim strsql As String
Dim strconn As String
Dim objcommand As SqlClient.SqlCommand ' 声明一个SqlCommand变量
Dim objdr As SqlClient.SqlDataReader ' 声明一个SqlDataReader变量
Dim str1, str2 As String
i = i – 1 ' 每稿裤森次单击登录,计数器减1
If i = 0 Then ' 如果登录失败次数超过3次
MessageBox.Show("您已经没有登录机会了,再见!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Me.Close()
Else ' 如果失败小于3次,则与数据库连接
strconn = "data source=cheng;initial catalog=学生成绩管理系统;user id=sa;password=123456;"
strsql = "select 用户名,密码 from 用户表"
objcommand = New SqlClient.SqlCommand(strsql, New SqlClient.SqlConnection(strconn))
objcommand.Connection.Open()
objdr = objcommand.ExecuteReader(CommandBehavior.CloseConnection)
With objdr
Do While .Read = True
str1 = .GetString(0)
str2 = .GetString(1)
If Trim(TextBox1.Text) = Trim(str1) And Trim(TextBox2.Text) = Trim(str2) Then
' 如果登录成功,进入主窗体
MessageBox.Show("恭喜您登录成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Visible = False
f.Show()
Exit Sub
End If
Loop
End With
End If
MessageBox.Show("登录失败!" + "您还有" + Str(i) + "次登录机会!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error) ' 如果登录失败,提示并重新
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub