Search code examples
.netvb.netms-accessoledb

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()

Solution

  • 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 :-).