vb.net - 如何将登录按钮与mysql数据库连接
问题描述
我的代码是一个登录表单,由于代码中的一些错误,它无法运行。我不知道如何调试它。我是编程初学者
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Cmdsignin.Click
If Txtusername.Text = "" Then
MessageBox.Show("Please Input the Username", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
Exit Sub
End If
If Txtpassword.Text = "" Then
MessageBox.Show("Please Input the password", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
Exit Sub
End If
Try
Dim DataRead As OdbcReader
Dim CmDSave As New OdbcCommand
With CmDSave
.Connection = dbconnection
.CommandText = "SELECT * FROM users WHERE username= '" & Txtusername.Text & "' and password= '" & Txtpassword.Text & "', conn"
.Parameters.Add("p1", Odbc.OdbcType.VarChar, 45).Value = Txtusername.Text
.Parameters.Add("p1", Odbc.OdbcType.VarChar, 45).Value = Txtpassword.Text
DataRead = .ExecuteNonQuery()
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
End Try
If DataRead.HasRows() Then
MessageBox.Show("Login success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
MDIMenu.Show()
Me.Close()
Else
MsgBox("Invalid Login information.!", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
dbconnection.Close()
End Sub
解决方案
我想知道您使用的需要 ODBC 的数据库。我已将用户界面代码与数据库代码分开。我不确定 anOdbcReader
是什么。你有这个名字的自定义类吗?
需要处理命令和连接等数据库对象。Using...End Using
即使有错误也会处理。这些对象应在使用它们的方法中声明,以便可以使用 Using 块。
您可以将连接字符串传递给 Connection 构造函数,并将 and 传递CommandText
给Connection
命令的构造函数。
Select 查询不是 NonQuery。ExecuteNonQuery
与插入、更新和删除命令一起使用。
您的参数没有用,因为它们没有出现在CommandText
.
Private ConnStr As String = "Your connection string"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Txtusername.Text = "" Then
MessageBox.Show("Please Input the Username", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
Exit Sub
End If
If Txtpassword.Text = "" Then
MessageBox.Show("Please Input the password", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
Exit Sub
End If
Dim Success As Boolean
Try
Success = IsLoginValid(Txtusername.Text, Txtpassword.Text)
Catch ex As Exception
MessageBox.Show(ex.Message, "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
Exit Sub
End Try
If Success Then
MessageBox.Show("Login success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
MDIMenu.Show()
Me.Close()
Else
MessageBox.Show("Invalid Login information.!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Function IsLoginValid(UName As String, PWord As String) As Boolean
Dim dt As New DataTable
Using dbconnection As New OdbcConnection(ConnStr),
CmDSave As New OdbcCommand("SELECT * FROM users WHERE username= p1 and password= p2;", dbconnection)
CmDSave.Parameters.Add("p1", Odbc.OdbcType.VarChar, 45).Value = UName
CmDSave.Parameters.Add("p2", Odbc.OdbcType.VarChar, 45).Value = PWord
dbconnection.Open()
Using reader = CmDSave.ExecuteReader
dt.Load(reader)
End Using
End Using
If dt.Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
这可以更容易地完成,但在不知道您需要什么风格的 sql 的情况下,我单独留下了选择。
推荐阅读
- javascript - 如果未启用原理图,如何将节点模块添加到 Angular 项目?
- http-live-streaming - medialement.js 不适用于我在 iPhone 上的 hls
- python-3.x - 解码 Python 字符串
- python - BurpSuite 上的 POST 请求响应与 Python 不同
- java - 确定文件是否在 Java 中被 PGP 加密
- ios - 如何使单元格中的某些文本或单词不可见或可见?
- go - 如何使用 Golang 获取发布的二进制数据的文件名?
- android - 没有来自服务器的填充无法加载广告:3
- angular - Angular 5 Material 如何将下拉菜单添加到多级轮播样式的 SideNav?
- php - PHP 回显 5000 条记录并取消设置