I have a sub in which I open sql databases through a OpenFileDialog. The problem arises when I try to re-open a database it was open before. Let me put an example: I first open the database "mydatabase.mdf" -> Result OK Then I open a second database "Seconddatabase.mdf" -> Result OK Now, when I try to re-open "mydatabase.mdf", OpenFileDialog triggers an error and say "This file is being used. Write another name". Well, the message is not this,this is my translation into English, because in my system it is in Spanish "Este archivo está en uso. Escriba un nuevo nombre o cierre el archivo abierto en otro programa". Well, the problem I am absolutely sure is caused by OpenFileDialog, because if I try the same process with an SaveFileDialog there is no file locking and I can open and reopen the database, but it's not a solution because the Dialog says the user to save file, not open. I did another check by inserting a button control in which I re-open the first database hard-coded and it works, so the problem is in OpenFileDialog. This is my code, but the code is not the problem (I think):
Private Sub AbrirBaseDados()
Dim fildia As New OpenFileDialog
If fildia.ShowDialog() Then
Try
If fildia.FileName <> vbNullString Then
pathBaseDados = (fildia.FileName)
CadenaConexion = "Data Source=(LocalDB)\v11.0;AttachDbFilename=" & pathBaseDados & ";Integrated Security=True;Connect Timeout=30"
End If
Catch ex As Exception
End Try
Desconectar()
fildia.Dispose()
End If
End Sub
The sub doesn't throw an exception, it's just OpenDialogFile telling me that the file is being used and not allowing me to get the name of the file I want to open. Because ALL I WANT TO KNOW is the name of the file of the db that the user specifies, because knowing the name I can open it without problem.
Thank you very much in advance.
Mdf is database file, not an ordinary file.
If you want to perform read write operations on database then read some tutorials first:
If you just want to get the names of database files from a given directory then use:
Dim filePaths As String() = Directory.GetFiles(YourFolderPath, "*.mdf")
Edit:
Use openFileDialog.ValidateNames = False
:
Dim fildia As New OpenFileDialog
fildia.ValidateNames = False