I have a VB.Net program running on vista that says a file is in use but the same program on XP does not have that error.
A:
Download Process Explorer - you can use that to determine which process has the file in question open.
Will A
2010-08-25 05:52:06
Sorry, to expand, the program opens an sql connection then when I open a new mdf file I close() the old connection and open() the new connection. This is fine. When I try to open() the first connection I get a "file in use" error, but only when I use the vista machine, not when I use the XP machine??
Brad Henderson
2010-08-25 06:01:51
@Brad Henderson, could you show your code of connecting to SQL?
Darin Dimitrov
2010-08-25 06:02:58
Public conn As New SqlConnectionPublic FileName As String = "c:\BAAW\Data\BAAW.mdf" Public connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" Integrated Security=True;User Instance=True"conn.ConnectionString = connStringconn.Open()and when I finish with it I do this:conn.Dispose()conn.Close()
Brad Henderson
2010-08-25 06:15:20
Does Process Explorer show that the file is in use by another application?
Will A
2010-08-25 06:19:35
Will A: I have Process Explorer running but can't find a reference to file usage??
Brad Henderson
2010-08-25 06:26:23
Will A, if you are still out there, you were correct in that Process Explorer did tell me. Except it wasn't my VB.NEt program that was holding the file but the sqlservr.exe that was holding it. Do you know how to force sqlservr to release a file?
Brad Henderson
2010-08-31 03:21:49
A:
Make sure you wrap disposable resources in Using statement to ensure they are properly released:
Using conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=c:\BAAW\Data\BAAW.mdf;Integrated Security=True;User Instance=True")
Using cmd As conn.CreateCommand()
conn.Open()
cmd.CommandText = "SELECT * FROM Employees"
Using rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Console.WriteLine(rdr(0))
End While
End Using
End Using
End Using
Darin Dimitrov
2010-08-25 06:26:47
Thanks Darin, I'll do a rewrite and see if that fixes. Appreciate your help
Brad Henderson
2010-08-25 06:53:21