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

An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll

**我在 Da.Fill(Ds, "tbl_user") 处遇到了这个错误: System.Data.dll

中发生类型为 'System.AccessViolationException' 的未处理异常

附加信息:试图读取或写入受保护的内存。这通常表明其他内存已损坏。 **

Public Conn As OdbcConnection
    Public Da As OdbcDataAdapter
    Public Ds As DataSet
    Public Dr As OdbcDataReader
    Public CMD As OdbcCommand

    Public MyDB As String


    Public Sub Connection()
        MyDB = "Driver={Mysql ODBC 5.1 driver};database=uas;server=localhost;uid=root"
        Conn = New OdbcConnection(MyDB)
        'jika koneksinya tertutup maka akan di open
        If Conn.State = ConnectionState.Closed Then Conn.Open()
    End Sub
 Private Sub FormMasterAdmin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call closeField()
        Call Connection()
        Da = New OdbcDataAdapter("Select username,fullname,level From tbl_user", Conn)
        Ds = New DataSet
        Da.Fill(Ds, "tbl_user")
        DataGridView1.DataSource = Ds.Tables("tbl_user")
        DataGridView1.ReadOnly = True
    End Sub

在代码文件的顶部添加...

Imports MySql.Data.MySqlClient

Call关键字只在特殊情况下使用。通常不需要。

将您的数据库对象保存在使用它们的方法的本地。需要关闭和处置数据库对象。 Using...End Using 块会为您处理这个问题。在此代码中,连接和命令都包含在 Using 块中。请注意 Using.

第一行末尾的逗号
Private MyDB As String = "Driver={Mysql ODBC 5.1 driver};database=uas;server=localhost;uid=root"

Private Sub FormMasterAdmin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt As New DataTable
    Using Conn As New MySqlConnection(MyDB),
            cmd As New MySqlCommand("Select username, fullname, level From tbl_user", Conn)
        Conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    DataGridView1.DataSource = dt
    DataGridView1.ReadOnly = True
End Sub

您必须将连接字符串更正为 MySql 提供商要求的格式。参见 https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/