I'm using Visual Studio Expess 2010. I am trying to figure out how to use the openfiledialog to getthe path of my database.

Currently I close the connection to my database and then try to select the database in the open file dialog but Windows tells me: "This file is in use, close the file..."

If I've closed my db connection what else do I need to do to completely close the file so I can select it in the openfiledialog?

有帮助吗?

解决方案

Closing the connection doesn't leave the Stream created to the file. Dispose the object you are using to set up the connection.

其他提示

It can depend a lot on where the db file is, e.g. is it in your project? is it in your sql server data folder?

Try the following sql to give you the path if it's in the data folder...

select physical_name
from sys.database_files
where type = 0

if it's in the app_data folder of your project try a simple

Server.mappath

EDIT: If you insist on using the dialogue then try the following:

openFileDialog.ValidateNames = false;

See this MS bug report

"First: The dialog ALWAYS checks for invalid characters, even if ValidateNames is FALSE.

Second: if TRUE, dialog tries to open the file and warns if locked"

EDIT 2: To understand exactly why the database is still in use - taken from here

"The sqlservr.exe process that is started is kept running for a while after the last connection to the instance is closed. Therefore, it doesn't need to be restarted if another connection is opened. The length of time it stays around is set by the sp_configure option "user instance timeout". By default, this is set to 60 minutes but you can use thesp_configure command to change this."

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top