shilpa sonawane

shilpa sonawane

  • NA
  • 8
  • 14.3k

To write dynamic connection string(c#2008,SQL SERVER 2005)

Mar 7 2012 8:23 AM
I have used Attach a database file, located in the data directory, on connect to a local SQL Server Express instance.code is
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\carRent.mdf;Integrated Security=True;User Instance=True";
return conn;
But I get error :

Unable to open the physical file "D:\CarRent Completed\CarRent\bin\Debug\carRent.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
An attempt to attach an auto-named database for file D:\CarRent Completed\CarRent\bin\Debug\carRent.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
how to solve this error?Thanks for your time and consideration.

Answers (1)

2
moji kashani

moji kashani

  • 0
  • 3
  • 1.3k
Mar 9 2012 10:17 AM
you can test these solutions:

1. check if there isn't another database which attached with the same name to your Sql Express(.\SqlExpress)

2. check if the carRent.mdf is not attached to another instance of Sql Server

-------------------------------------------------------------------------------
if you couldn't solve your problem after those step then

check if you can attach this file manually to your Sql Express. If you couldn't attach it, so you problem will be simpler and if you could, it's mean that you have some problem with your codes


hope be helpful.