gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:37:33 24 4
gpt4 key购买 nike

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

我听说执行此操作的通常方法是只有一个名为“[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 - vb.net应用程序使用mysql数据库登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8705331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com