none
VB2005编写的登陆程序出现异常,请问如何解决? RRS feed

  • 问题

  • 我在VB中建立一个登陆窗体,代码如下:

    Imports System.Data.SqlClient
    Imports System.Data
    Public Class login
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim user, password, sqlstr As String
            User = TextBox1.Text.Trim
            password = TextBox2.Text.Trim
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MsgBox("帐号或密码不得为空!", MsgBoxStyle.OkOnly + 48, "登录失败")
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox1.Focus()
            Else
                sqlstr = "select * from userinfo where user_id='" & User & "' and user_pwd='" & password & "'"
                Dim objdatarow As DataRow
                Try
                    objdatarow = getdatafromdb(sqlstr).Tables(0).Rows(0)
                Catch
                    MsgBox("密码和账号不符,请重试,并检查该账号是否存在。", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "系统登录失败")
                    Me.TextBox2.Text = ""
                    Exit Sub
                End Try
                username = user
                pepdom = objdatarow.Item(2)
                Dim frmmain As New main()
                frmmain.Show()
                Me.Finalize()
            End If
        End Sub

    另外附上模块一的代码:

    Imports System.Data
    Imports system.dbnull
    Imports System.Data.sqlclient
    Module Module1
        Public username As String        '用户名
        Public pepdom As Integer         '用户等级
        Public CL As Integer             'CL取不同的值对应着对员工信息的不同操作(添加,修改等)
        Public M As Integer              'M取不同的值对应着对菜品信息的不同操作(添加,修改等)
        Public c_id As String = ""       '表示被选中的员工编号
        Public m_id As String = ""       '表示被选中的菜品编号
        Public EN As Boolean             '表示是否已经从enorderinfo中选择了账单信息
        Public o_id As String            '表示被选中的账单编号
        Public SU As Double              '账单的菜品总价格
        Public objdataset As DataSet
        Public objdataview As DataView
        Public objdataadapter As SqlDataAdapter
        Public objsqlconnection As SqlConnection = New SqlConnection("data sourse=LX-140\SQLEXPRESS;initial catalog=Foodservice;integrated secturity=sspi")
        Public objcurrencymanager As CurrencyManager
        Public usersqlstr As String = "select * from userinfo"

        Public Function getdatafromdb(ByVal sqlstr As String) As DataSet    '该函数实现通过给定的SQL语句将相应的数据映射到数据适配器
            objdataset = New DataSet()
            objdataset.Clear()
            Try
                objsqlconnection.Open()
                objdataadapter = New SqlDataAdapter(sqlstr, objsqlconnection)
                objdataadapter.Fill(objdataset)
                objsqlconnection.Close()
            Catch ex As Exception
                MsgBox("错误号:" & Err.Number & "错误描述:" & Err.Description)
            End Try
            Return objdataset
            objdataset = Nothing
        End Function

        Public Function updatedata(ByVal sqlstr As String) As Boolean    '该函数会根据指定参数的SQL语句实现与数据库的交互
            Dim objcommand As SqlCommand = New SqlCommand()
            Try
                objsqlconnection.Open()
                objcommand.Connection = objsqlconnection
                objcommand.CommandText = sqlstr
                objcommand.CommandType = CommandType.Text
                objcommand.ExecuteNonQuery()
                objsqlconnection.Close()
            Catch ex As Exception
                MsgBox("错误号:" & Err.Number & "错误描述:" & Err.Description)
                Return False
            End Try
            Return True
        End Function
    End Module

    然后我USERINFO表中的字段有USER_ID(用户名),USER_PWD(用户密码),USER_PEPDOM(用户等级).

    ------------------------------------------

    我的问题是每次当我运行程序准备登陆的时候总会弹出"密码和账号不符,请重试,并检查该账号是否存在。",可是我确信自己密码和帐户没有输错.另外如果我去掉TRY语句,则会显示“WindowsApplication1.Module1”的类型初始值设定项引发异常。

    请问大家如何解决这个问题?万分感谢!!!

    2012年12月3日 11:44

答案

  • @另外如果我去掉TRY语句,则会显示“WindowsApplication1.Module1”的类型初始值设定项引发异常。

    说明你的程序已经有异常存在,只不过被你的Try……Catch……给拦截了。所以调试程序去掉这些块。

    我补充最最关键的部分:

    Using cmd As New SqlCommand("select count(*) from xxx where user_name=@name and user_pwd=@pwd,New SqlConnection("你的字符串链接"))
    
          cmd.Parameters.AddWithValue("@name",your value 1)
          cmd.Parameters.AddWithValue("@pwd",your value 2)
          cmd.Connection.Open()
         Dim result As Integer = cmd.ExecuteScalar()
       
          If (result>0) Then
           '做成功登陆的事情
           Else
           '做失败处理
          End If
    
    End Using

    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心


    2012年12月4日 2:12
    版主