使用 Vb.net 和 Mysql 作为数据库登录的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21869657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:05:04  来源:igfitidea点击:

codes for Login using Vb.net and Mysql as database

mysqlvb.netauthentication

提问by anayaur30

please help to debug my codes. it is a login form , it cant run because of some errors in codes. please help me.. i dont know how to debug it.
i am a beginner in programming.

请帮助调试我的代码。它是一个登录表单,由于代码中的一些错误而无法运行。请帮助我..我不知道如何调试它。
我是编程的初学者。

Imports MySql.Data.MySqlClient

Public Class Form1

     ' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cmd As New MySqlCommand
        Dim da As New MySqlDataAdapter
        Dim con As MySqlConnection = jokenconn()
    End Sub'

    Public Function jokenconn() As MySqlConnection
        Return New MySqlConnection("server=localhost;user id=root;Database=db_ko;")
    End Function

    Private Sub cmdlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlogin.Click
        Dim connString As String = "Database=db_ko;Data Source=localhost;User ID=root;"

        Dim conn As New MySqlConnection(connString)
        Dim cmd As New MySqlCommand

        Dim user As String = txtusername.Text
        Dim pass As String = txtpass.Text
        Dim reader As DataTableReader

        conn.Open()
        cmd.CommandText = "SELECT * FROM `db_account` WHERE username= '" & txtusername.Text & "' and password= '" & txtpass.Text & "', conn"
        cmd.Connection = conn

        cmd.Parameters.Add(New MySqlParameter("?UserName", txtusername.Text))
        cmd.Parameters.Add(New MySqlParameter("?Password", txtpass.Text))

        cmd.Connection = conn
        'cmd.EndExecuteReader = reader

        If reader.HasRows() Then
            MessageBox.Show("Login success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Form2.Show()
            Me.Close()

        Else

            MsgBox("Invalid Login information.!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        conn.Close()

    End Sub

End Class

sorry for the codes are wrong. thank you advance!!

抱歉代码错误。先谢谢了!!

回答by Codemunkeee

you have commented out this line here. Remove " ' "

您已在此处注释掉这一行。消除 ” ' ”

' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

and change it to

并将其更改为

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

also, try removing the "`" on db_account on this line

另外,尝试删除此行上 db_account 上的“`”

cmd.CommandText = "SELECT * FROM `db_account` WHERE username= '" & txtusername.Text & "' and password= '" & txtpass.Text & "', conn"

to

cmd.CommandText = "SELECT * FROM db_account WHERE username= '" & txtusername.Text & "' and password= '" & txtpass.Text & "', conn"