1

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()
ADyson
  • 57,178
  • 14
  • 51
  • 63
Cz Raven
  • 33
  • 8
  • is there any way to check if dtb is using another process? – Cz Raven Aug 12 '19 at 14:17
  • Try to put all your code in a Try Catch block. Look at InnerException, if any. Another thing you can try is move your database to another folder. – RobertBaron Aug 12 '19 at 18:29
  • Please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime. – Mary Aug 12 '19 at 21:01
  • Is your table name MYDTB the same as the file name of the database? – Mary Aug 12 '19 at 21:21
  • it was change for public help :-) – Cz Raven Aug 12 '19 at 21:40
  • nothing help :-/ Still have same problem: "System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'" // Can this be caused by multiple connections to the same database? – Cz Raven Aug 12 '19 at 21:44

2 Answers2

2

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
Mary
  • 14,926
  • 3
  • 18
  • 27
  • Hi Mary i try your code but i have still problem with read / write protected memory. Can this be caused by multiple connections to the same database? very Thx – Cz Raven Aug 12 '19 at 22:09
  • 1
    Sorry, I was afraid that would happen. There are a bunch of answers here https://stackoverflow.com/questions/4074585/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-ot/16974074 Maybe the one with 44 upvotes will help. You are not the only one with this error! – Mary Aug 12 '19 at 22:13
  • Also found "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\DbTest\Test.accdb; OLE DB Services=-1" The addition of OLE DB Services = -1 seemed to do the trick. I Googled "trying to connect to Access error read / write protected memory" and there are lots of links to look at. – Mary Aug 12 '19 at 22:28
2

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

Cz Raven
  • 33
  • 8