MySql - System.Data.dll 中发生类型为“System.InvalidOperationException”的未处理异常

开发者

我正在使用 MYSQL 数据库...我收到以下错误...请任何人都可以帮助我?

Imports System.Data
Imports MySql.Data Imports MySql.Data.MySqlClient
Public Class formLogin
  Dim connStr As String = "server=localhost;user=root;database=ssknet;port=3306;password=;"
  Dim connection As New MySqlConnection(connStr)

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    connection.Open()
    Dim cmd As New MySqlCommand("SELECT * FROM user WHERE username=@username and password=@password", connection)


    cmd.Parameters.Add("@username", MySqlDbType.VarChar).Value = txtUsername.Text
    cmd.Parameters.Add("@password", MySqlDbType.VarChar).Value = txtPassword.Text

    Dim adapter As New MySqlDataAdapter
    Dim table As New DataTable

    adapter.Fill(table)

    If table.Rows.Count <= 0 Then
        MessageBox.Show("Invalid Username or Password")
    Else
        MessageBox.Show("Login Success!")
    End If

    'cmd.ExecuteNonQuery()
    connection.Close()
End Sub

结束类

我现在想访问数据库。快速查询

史蒂夫

你错过了一个关键步骤。您没有向适配器分配任何命令。它不知道如何在没有命令的情况下查询任何内容

Dim adapter As New MySqlDataAdapter(cmd)

接下来,根据MySql Reserved KeyWords,用户和密码是保留的,要在查询中将它们用作字段名称,您应该在它们周围加上反引号 (ALT+096)。所以查询应该写成

Dim cmd As New MySqlCommand("SELECT * FROM `user` 
                            WHERE username=@username 
                            AND `password`=@password", connection)

尽管有几件事情需要考虑,但您的代码已经足够好了。

首先,连接对象不应该作为全局对象保存。这会导致服务器上保留的资源以及出现错误时关闭/打开连接的许多问题。只需在 using 语句中创建和丢弃连接

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Using connection = New MySqlConnection(connStr)
   connection.Open()

   .... all of your code except the close connection

End Using ' this close the connection also in case of exceptions

其次,如果您只想检查用户/密码是否存在,则不需要 SqlDataAdapter 和数据表

Dim reader = cmd.ExecuteReader()
if reader.HasRows then
    MessageBox.Show("Login Success!")
Else
    MessageBox.Show("Invalid Username or Password")
End If

第三,将密码以纯文本形式保存在数据库中然后使用查询来检索它是一个很大的安全风险。您应该使用Salt 和 Hashing方法来存储和检索密码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Windows 窗体“System.Data.dll 中发生类型为‘System.ArgumentException’的未处理异常”

System.Data.dll 中发生类型为“System.AccessViolationException”的未处理异常

从文本框写入sql server 2012时,System.Data.dll中发生了类型为'System.InvalidOperationException'的未处理异常

WebDriver.dll中发生类型为'System.InvalidOperationException'的未处理异常

System.Data.Linq.dll中发生了类型为'System.Data.SqlServerCe.SqlCeException'的未处理异常

VisualStudio错误:System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常

错误:System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常

System.Data.dll中发生了'System.ArgumentException'类型的未处理异常

VimService55.XmlSerializers.dll中发生类型为'System.StackOverflowException'的未处理异常

错误:mscorlib.dll中发生了类型为'System.UnauthorizedAccessException'的未处理异常

Microsoft.WindowsAzure.Storage.dll中发生了类型为'System.StackOverflowException'的未处理异常

EntityFramework.dll中发生类型为'System.Data.Entity.ModelConfiguration.ModelValidationException'的异常

选择查询 C# 中发生类型为“System.Data.OleDb.OleDbException”的未处理异常

错误:System.Windows.Forms.dll中发生了类型为'System.BadImageFormatException'的未处理异常

为什么我在System.Core.dll中发生类型为'System.ArgumentNullException'的未处理异常

生成报告会导致在system.windows.forms.dll中发生类型为'system.stackoverflowexception'的未处理异常

System.Web.Extensions.dll中发生类型为'System.InvalidOperationException'的异常,但未在用户代码中处理

在System.Drawing.dll中发生了'System.ArgumentException'类型的未处理异常,用于灰度

Windows窗体应用程序数据库:“ System.Data.dll中发生了'System.ArgumentException'类型的未处理的异常”

SetAccessControl提供错误,因为mscorlib.dll中发生类型为'System.UnauthorizedAccessException'的未处理异常

File.Copy,mscorlib.dll 中发生类型为“System.IO.IOException”的未处理异常

EntityFramework.dll中发生类型为'System.InvalidOperationException'的异常,但在注册时未在用户代码中处理

System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常

mscorlib.dll中发生了'System.ComponentModel.Win32Exception'类型的未处理异常

BLL.dll中发生了'System.ArgumentException'类型的未处理异常

System.Windows.Forms.dll 中发生类型为“System.InvalidOperationException”的第一次机会异常

ASP.NET WebAPI错误:EntityFramework.dll中发生类型为'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理

在View中使用2个模块:System.Web.Mvc.dll中发生类型为'System.InvalidOperationException'的异常,但未在用户代码中处理

System.Core.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理