注册 登录
编程论坛 VB.NET论坛

用户登陆问题求助

wangkq936 发布于 2008-11-26 10:50, 1313 次点击
本人学习学做一个登陆窗体,运行时提示用户名或密码错,请大家帮我看下是哪个地方写错了,以下是原码:
Public Class Form3
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cnStr As String, cmdtext As String
        cnStr = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath() & "\database.mdb"
        Dim cn As New OleDbConnection(cnStr)
        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader
        Dim dt As New DataTable("user")
        Try
            cn.Open()
            cmdtext = "select * from user where name='" & nametext.Text & "' and password='" & pwtext.Text & "'"
            cmd = New OleDbCommand(cmdtext, cn)
            dr = cmd.ExecuteReader
            If dr.HasRows Then
                MessageBox.Show("登陆成功", "系统登陆", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                dr.Close()
            End If

        Catch ex As Exception
            MessageBox.Show("用户名或密码错误", "系统登陆", MessageBoxButtons.OK, MessageBoxIcon.Information)
            nametext.Text = Nothing
            pwtext.Text = Nothing
            nametext.Focus()

        Finally
            cn.Close()
            cn = Nothing
        End Try
    End Sub
6 回复
#2
wangkq9362008-12-08 13:36
论坛太够意思了,发了这么久都没有人回应下,管理员去哪里了?该罚了!
#3
ggvboy2008-12-08 15:59
回答:
一、查查百度就知道每天有多少人问登录的问题了,VS给了一个Debug的功能也不是摆设。
二、user这个名字好像不合法,我试个userInfo就没事了
#4
wangkq9362008-12-09 11:52
请问楼上,你是用VS什么版本来试的,我用2005/2008都试过了结果还是一样,能不能把试好没有总是的原代码发给我,我调了很久都没有搞定,我的邮箱是wangkq-936@,谢谢你了,论坛就是用来学习交流的是吗!
#5
ggvboy2008-12-09 16:20
我用的是2005日文版的
你这样定义
Dim cn As New OleDbConnection(cnStr)肯定是不行
应该
Dim cn As New OleDb.OleDbConnection(cnStr)
代码我给你发过去了
#6
wangkq9362008-12-16 15:23
搞定了,谢谢你的帮助,
#7
wangkq9362008-12-16 15:37
楼主 wangkq936 的帖子
跟大家一起分享登陆问题的,VS2005下调试正常的代码:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        Dim cnStr As String, cmdtext As String
        cnStr = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath() & "\database.mdb"
        Dim cn As New OleDb.OleDbConnection(cnStr)
        Dim cmd As OleDb.OleDbCommand
        Dim dr As OleDb.OleDbDataReader
        Dim dt As New DataTable("userInfo")
        Try
            cn.Open()
            cmdtext = "select * from userInfo where name = '" & txtName.Text.ToString() & "' and password = '" & txtPassword.Text.ToString() & "'"
            cmd = New OleDb.OleDbCommand(cmdtext, cn)
            dr = cmd.ExecuteReader
            If dr.HasRows Then
                MessageBox.Show("登陆成功", "系统登陆", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("用户名或密码错误", "系统登陆", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtName.Text = Nothing
                txtPassword.Text = Nothing
                txtName.Focus()
            End If
        Catch ex As Exception
             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            cn.Close()
            cn = Nothing
        End Try
1