当前位置:编程学习 > C#/ASP.NET >>

VB,NET在编写用户登录是出错提示“在没有任何数据时进行无效的读取尝试”

代码是:
Imports System.Data
Imports System.Data.SqlClient

Public Class Login

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("用户名和密码不能为空!")

        Else
            Dim cn As New SqlConnection
            Dim password As String
            Dim name As String

            Dim da As SqlDataReader
            name = TextBox1.Text
            cn.ConnectionString = "server=.;uid=sa;pwd=123;database=索尼手机专卖店数据库"

            cn.Open()
            If cn.State = ConnectionState.Open Then
                Dim aa As String
                aa = "select * from 用户表 where 用户名='" & name & "' and 密码='" & TextBox2.Text & "'"
                Dim cmd As New SqlCommand
                cmd.CommandText = aa
                cmd.Connection = cn
                da = cmd.ExecuteReader
                da.Read()
                password = da.GetString(1)
                If TextBox2.Text = password Or password = "" Then
                    MsgBox("    登录成功!欢迎使用系统!", vbExclamation, "登录成功!")
                    Main.Show()
                Else
                    MsgBox("    登录失败!拒绝访问系统!", vbExclamation, "登录失败!")



                End If
            End If
        End If
    End Sub
调试时我用正确的用户名,错误的密码时在 password = da.GetString(1)提示出错““在没有任何数据时进行无效的读取尝试”。为的是验证打正确的用户名时,输入错误的密码,然后提示MsgBox("    登录失败!拒绝访问系统!", vbExclamation, "登录失败!")这一行。
在线求各位回答。。。 --------------------编程问答-------------------- 做课程设计啊,各位大人帮帮忙啊。。。 --------------------编程问答-------------------- 你密码都输错了
 aa = "select * from 用户表 where 用户名='" & name & "' and 密码='" & TextBox2.Text & "'"
这能有结果???

da 怎么可能会有东西。没有东西你怎么弄下一条 da.Read()

if da.Read = True then

 MsgBox("    登录成功!欢迎使用系统!", vbExclamation, "登录成功!")
else
 MsgBox("    登录失败!拒绝访问系统!", vbExclamation, "登录失败!")
end if 
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,