顺丰快递公司规模:请问在vb.net中如何把sql中的数据读取到textbox中?

来源:百度文库 编辑:高校问答 时间:2024/04/28 15:04:23
这是我的程序,不过运行后,点击[button6]按钮,总是提示"在没有任何数据时进行无效的读取尝试".希望有高手能指点迷津,谢谢
PS:插入数据是可以的,但是读取就不行了.

Private Const linkstring As String = "Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=mysql;Data Source=C506"
Dim link As New System.Data.SqlClient.SqlConnection(linkstring)

'上面的是先做一下准备工作,定义一些变量

Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'点击按钮的程序
Dim sqlcode As String
sqlcode = "select 电压 from table1"
Dim execute As New System.Data.SqlClient.SqlCommand(sqlcode, link)
Dim myReader As System.Data.SqlClient.SqlDataReader = execute.ExecuteReader()
If link.State = ConnectionState.Open Then
Try
Dim a As String = myReader.GetValue(1)
'上面的getvalue(1)我先后换过getvalue(0),getstring(0),getstring(1),都不行
Catch e6x As Exception
MsgBox(e6x.Message)'取sql错误提示,那个[在没有任何数据时进行无效的读取尝试]就是打这儿来的

End Try
Else
MsgBox("ERROR")
End If
myReader.Close()
End Sub

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