6

I am using Visual Studio 2010 C# Express and SQL Server Management Studio with SQL Server 2012 I am getting an error when I try to connect a database to my Windows forms project.

"This file is in use.  Enter a new name or
close the file that is open in another program"

I poked around online to try to find a solution but none worked. So i decided to create a brand new database and a brand new project. But I still get the error!

here are the steps I use:

  • First I connected to my locally hosted server
  • then i right-clicked on Databases and selected New Database and named it school
  • then i right clicked the tables folder on my new database and selected new table
  • I added thes tables student, course, enrolled
  • finally i threw some bogus entries into it

Now I jump over to VS

  • I select New Project then Windows Forms Application and name it Registrar
  • then i click add new data source
  • select database in the Choose a data source type window
  • then i select Dataset in the Choose a Database Model window
  • then I click New Connection I change the DataSource to Microsoft SQL Server Database File browse to the DATA folder inside my 'SQLEXPRESS' folder and when i click the 'school.mdf' file i get the above error.

any help would be greatly appreciated.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Mike
  • 437
  • 3
  • 8
  • 18

2 Answers2

16

Your school.mdf is attached in your SQL Server 2012. You need to detach it.

Go to your SQL Server Management Studio, Right-Click on School, Tasks, Detach.

Unless it is detached, it will be in use by the the SQL Server Management Studio.

tempidope
  • 823
  • 1
  • 12
  • 29
  • Thank you very much! I never would have figured that out. Is there a way that I can still add data to it through the Server Management Studio or do I need to work with it through VS only now? – Mike Nov 04 '12 at 15:38
  • Hmm.. So i was able to successfully include it in my project. But now I wanted to go back into Server Studio to edit and add new Stored Procedures. However, when I reattach the database, it doesnt have any of the data loaded into it that I add through VS. – Mike Nov 04 '12 at 16:12
  • 3
    Hey Mike, I just checked through the steps you're performing to create a data source. Could I suggest you to choose "Microsoft SQL Server" instead of "Microsoft SQL Server Database File" while creating a "New Connection"? If you do that, I believe you can directly connect to your SQL Express database without the headache of detaching/reattaching. – tempidope Nov 04 '12 at 16:18
  • I don't have that option. On the "Change Data Source" window the option i have are "Microsoft Access Database File", "Microsoft SQL Server Compact 3.5" and "Microsoft SQL Server Database File" . If i chose the "Compact 3.5" there are no files listed in my server to choose. – Mike Nov 04 '12 at 16:29
  • Is it the same case when you Add a new data source? – tempidope Nov 04 '12 at 17:02
  • Yes, when i add a new data source, those are still the only options I am getting. – Mike Nov 04 '12 at 18:13
4

You need To detach it. If you don't want to do that, select "Microsoft SQL Server" instead of "Microsoft SQL Server Database File". Express versions VB don't have Microsoft SQL Server option.