Cz Raven
Cz Raven

Reputation: 33

VB.NET Access - Attempted to read or write protected memory

I have a problem where my code is generating this error:

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This occurs at the line My_CONNECTION.Open().

Any idea how to fix this?

My code:

vb.net

Dim My_USER As String = Environment.UserName
Dim My_CONNECTION As New OleDbConnection
Dim My_DATABASE As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\" & My_USER & "\AppData\Roaming\MYAPP\MYDTB.accdb;Persist Security Info=False;"
Dim My_COMMAND As New OleDbCommand
Dim My_READER As OleDbDataReader
Dim My_COUNT As Integer

'### Open connection ###

My_CONNECTION.ConnectionString = My_DATABASE
My_CONNECTION.Open()

'### SQL ###

With My_COMMAND
.CommandText = "SELECT COUNT(*) FROM MYDTB"
.CommandType = CommandType.Text
.Connection = My_CONNECTION
End With

My_COUNT = My_COMMAND.ExecuteScalar()

'### Close connection ###

My_CONNECTION.Close()

Upvotes: 1

Views: 2235

Answers (2)

Cz Raven
Cz Raven

Reputation: 33

I change Provider from:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DbTest\Test.accdb;Persist Security Info=False;"

to:

 "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\DbTest\Test.accdb; OLE DB Services=-1"

only added: "OLE DB Services=-1" and all working good now :-).

Upvotes: 2

Mary
Mary

Reputation: 15081

I don't see anything wrong with your connection string of the location of the file. I would like you to correct some of your database code. I am afraid it will produce the same error but give it a try.

Database objects that expose a .Dispose method need to be disposed so that they can release unmanaged objects. Connections and Commands fall into this category. Using...End Using blocks will close and dispose these objects even if there is an error.

You can pass the Connection String directly to the constructor of the Connection. Nothing wrong with your way, it just shortens the code a bit. Likewise, the Command constructor can be passed the Command Text and the Connection.

Option Strict will tell you that .ExecuteScalar() returns and object and it cannot be implicitly converted to an Integer. We are sure it will be an Integer so just use CInt()

Connections should be opened at the last minute and closed as soon as possible. The End Using closes the connection.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim My_USER As String = Environment.UserName
    Dim My_COUNT As Integer
    Using My_CONNECTION As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\" & My_USER & "\AppData\Roaming\MYAPP\MYDTB.accdb;Persist Security Info=False;")
        Using My_COMMAND As New OleDbCommand("SELECT COUNT(*) FROM MYDTB", My_CONNECTION)
            My_CONNECTION.Open()
            My_COUNT = CInt(My_COMMAND.ExecuteScalar())
        End Using
    End Using
    Label1.Text = My_COUNT.ToString
End Sub

Upvotes: 2

Related Questions