2

I'm using C# Express 2010 and SQL Server 2008 Express.

I want to create a connection to said server in the C# IDE (not in code) by going Database Explorer -> Right Click -> Add Connection, but it only gives me an option to connect to a file rather than a server name.

And even if I do try to connect to the file I get a "This file is in use" error thrown at me.

Can anyone tell me where I'm going wrong?

Jez Clark
  • 2,919
  • 4
  • 26
  • 31

5 Answers5

1

I don't know if you solved your problem or not, but I found a solution to get around the problem.

I found out on another forum that the Express version of C# Visual Studio does not let you connect to the 'live' SQL Express Server using a connection string. They want you to make a local copy of the database file and to use that instead. Microsoft do this 'by design' and is a limitation of the C# Express version. Don't disrepair though as you can get around the limitaton with a little effort. This is what you do:

  1. Make a connection to the database as you would normally do in Visual Web Developer. This will produce 2 files called something like Model1.edmx and Model1.Designer.cs

  2. In your Visual C# application create a EDO model, but this time create an empty model. Use the same name for the model (i.e. Model1).

  3. Copy the model files you created in Visual Web Developer over the top of the Visual C# 'empty' ones using Windows Explorer.

  4. Open the Web.Config file in Web Developer and copy the connectionStrings entries. Paste this into the App.Config file in the same place.

Everything should now work as expected, the good thing is that you can update the database fields when you change fields in the database tables by going to the model page, right clicking and do the update as you would normally do in Web Developer.

Mnemonic
  • 11
  • 1
1

Perhaps you have chosen the wrong kind of Datasource? It should say "Microsoft SQL Server (SqlClient)", not "Microsoft SQL Server Database File (SqlClient)".

You should then get a text box called "Server name" where you need to enter the name of you SQL Express instance. Most often, this should be ".\SQLEXPRESS"

CodingInsomnia
  • 1,843
  • 2
  • 15
  • 19
  • I only have options for "Microsoft Access Database File" and "Microsoft SQL Server Database File" - no option for "Microsoft SQL Server"...does that mean I'm missing something in my installation? – Jez Clark Feb 17 '11 at 14:07
  • Looking around I found this: http://stackoverflow.com/questions/188963/connecting-to-sql-server-with-visual-studio-express-editions It seems that it's simply not available for C# Express, but is available for Web Developer Express.. :-( – CodingInsomnia Feb 17 '11 at 14:10
  • Gah, looks like I'm back to the original plan of actually doing some _real_ work and coding it all myself then :P Thanks for the help. – Jez Clark Feb 17 '11 at 15:21
1

When you choose the datasource, you have to choose "Microsoft Sql Server" instead of "Microsoft Sql Server Database File".

Steve B
  • 36,818
  • 21
  • 101
  • 174
1

Did you change the Data source to "Microsoft SQL Server"?

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

Like most people facing this problem, I'm a novice with these Microsoft IDEs (Interactive Development Environments), but I've successfully used C# Express 2010 to access and update a database created in SS 2008 R2 Express, so I'll share what I did.

I never could figure out how to use the Data Source Wizard to make it work, but it was easy to just type the connection string and make the value of Data Source=.\SQLEXPRESS

Then I was able to work with a database in both SS 2008E and C# Express 2010 on a single Windows Professional computer.

I found this tutorial helpful for getting started with datasets. The tutorial avoided the challenge of connecting to a SS Express MDF from C# Express by creating its database from within C# Express; and, as of this writing, it is out-of-date on data connection screens (which I'm not certain can handle the problem anyway), but I thought it was a good intro to using C# to access and update SS data. If you use the tutorial, just build the table they suggest directly in SS Management Studio. Then ignore the steps for using the menus/wizards to create your connection string. Just type what they show in the code except make the Data Source = .\SQLEXPRESS

10 Rep
  • 2,217
  • 7
  • 19
  • 33