I have a small piece of code I am trying to run in which I first check if the value exists and then update my database if it does not. My problem is I can run one query or the other but not both, because then it states the database is open and cannot be accessed. I know for sure I can write to the database. It is not read protected, located where I can't access it etc. The routine runs if I just try to update and not check. The check definitely results in no record. I have tried separate routines using completely different variables for the connection. I am stumped big time. I have tried closing, using, disposing, but I need someone a lot smarter than me to advise me of what I am doing wrong...
For Each strfile As String In flist
Dim pth As String = Path.GetDirectoryName(strfile)
Dim objReader As New System.IO.StreamReader(strfile)
Dim dVal As String = String.Empty
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine, ",")
dVal = SplitLine(1)
Using Dcon As New OleDbConnection
Dcon.ConnectionString = dbProvider
Dcon.Open()
Dim q1 As String = "SELECT Shares.[_Date] FROM Shares WHERE (((Shares.[_Date])=" & dVal & "))"
Dim comd As OleDbCommand = New OleDbCommand(q1, Dcon)
comd.ExecuteReader()
Dim q2 As String = "INSERT INTO Shares (Code, _Date, _Open, _High, _Low, _Close, _Volume) " &
"SELECT F1, F2, F3, F4, F5, F6, F7 FROM [Text;HDR=NO;DATABASE=" & pth & "].[" & Path.GetFileName(strfile) & "];"
Dim cmd As OleDbCommand = New OleDbCommand(q2, Dcon)
cmd.ExecuteNonQuery()
Dcon.Close()
End Using
Next
I have tried separate routines, I have tried opening, closing and then re-opening. I have tried one connection, I have tried two. Does anybody know what I am doing wrong and why it keeps telling me the database is exclusively opened and why it won't let me write to it?
Thank you in advance to those who help.