繁体   English   中英

使用mysql数据库登录vb.net应用程序

[英]vb.net application login using mysql database

我试图让用户登录到我在vb.net中使用mysql中应用程序数据库的用户表创建的应用程序

我听说,通常的做法是只有一个具有相关权限的名为“ [my_app_name]”的MySQL用户。 然后,我的应用程序使用它自己的用户表来控制对应用程序的访问,并使用一个MySQL用户来访问数据库。 但我不知道该怎么做,有人可以帮我吗。 我对所有这些都是新的。

谢谢

如果您要创建一个登录表单,该表单将检查用户及其在MySQL数据库上的密码的身份验证,请按照我为自己的登录表单编写的代码进行操作。

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

    Dim conn As MySqlConnection

        'Connect to the database using these credentials
        conn = New MySqlConnection
    conn.ConnectionString = "your server goes here; user id=userid goes here; password=self explanatory; database=the database where the credential information is located"

        'Try and connect (conn.open)
        Try
            conn.Open()

        Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
        MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
        End Try


        'MySQL query (where to call for information)
        Dim myAdapter As New MySqlDataAdapter

        'Tell where to find the file with the emails/passes stored
    Dim sqlquery = "SELECT * FROM your database with info WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'"
        Dim myCommand As New MySqlCommand
        myCommand.Connection = conn
        myCommand.CommandText = sqlquery

        'Start query
        myAdapter.SelectCommand = myCommand
        Dim myData As MySqlDataReader
        myData = myCommand.ExecuteReader

        'See if the user exists
        If myData.HasRows = 0 Then
        MsgBox("Invalid email address or password.", MsgBoxStyle.Critical)

        'Insert your settings change here. (i.e. My.Settings.LoggedIn = False)

        Else
        MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information)

        'Another settings change: My.Settings.LoggedIn = True

            Me.Close() 'close the login form

    End If

确保更改控件名称并添加设置。

打开MySQL Workbench,在第一个屏幕的右下角的“服务器管理”列下,您将看到“管理安全性”。 点击那个。 在左侧菜单中,您将看到“用户和特权”。 点击那个。 您会看到一个显示“添加帐户”的按钮,单击该按钮将允许您创建一个新用户。 在“架构权限”选项卡中,您可以修改用户的权限。

您在此处创建的用户可以在您的连接字符串中使用。 这是来自我的一个应用程序的示例MySQL连接字符串。 下面的连接字符串“ repair”中的用户名是使用“登录名:”作为“修复”和“限制与主机匹配的连接:”作为“%”创建的。

“服务器= 10.0.0.113; uid =修复; pwd ='password&92';数据库=修复”

我希望这有帮助。 祝你有美好的一天。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM