what’s the issue with AttachDbFilename

Apparently, using AttachDbFilename and user instance in your connection string is a bad way to connect to a DB. I’m using SQL server express on my local machine and it all seems to work fine. But what’s the proper way to connect to SQL server then?

Thanks for your explanation.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Using User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database. This leads to a lot of confusion, as people will test updating data with their program, then connect to a different copy of their database in Management Studio, and complain that their update isn’t working. This sends them through a flawed series of wild goose chase steps trying to troubleshoot the wrong problem.

This article goes into more depth about how to use this feature, but heed the very first note: the User Instance feature has been deprecated. In SQL Server 2012, the preferred alternatives are (in this order, IMHO):

  1. Create or attach your database to a real instance of SQL Server. Your connection string will then just need to specify the instance name, the database name, and credentials. There will be no mixup as Management Studio, Visual Studio and your program(s) will all be connecting to a single copy of the database.
  2. Use a container for local development. Here’s a great starter video by Anna Hoffman and Anthony Nocentino, and I have some other resources here, here, and here. If you’re on an M1 Mac, you won’t be able to use a full-blown SQL Server instance, but you can use Azure SQL Edge if you can get by with most SQL Server functionality (the omissions are enumerated here).
  3. Use SqlLocalDb for local development. I believe I pointed you to this article yesterday: “Getting Started with SQL Server 2012 Express LocalDB.”
  4. Use SQL Server Compact. I like this option the least because the functionality and syntax is not the same – so it’s not necessarily going to provide you with all the functionality you’re ultimately going to want to deploy. Compact Edition is also deprecated, so there’s that.

Of course if you are using a version < SQL Server 2012, SqlLocalDb is not an option – so you should be creating a real database and using that consistently. I only mention the Compact option for completeness – I think that can be almost as bad an idea as using AttachDbFileName.

EDIT: I’ve blogged about this here:

Method 2

In case someone had the problem.

When attaching the database with a connection string containing AttachDBFile
with SQLEXPRESS, I noticed this connection was exclusive to the ASP.NET application that was using the database. The connection did block the access to all other processes on the file level when made with System.Data.SqlClient as provider.

In order to assure the connection to be shareable with other processes
instead use DataBase to specify the database name in your connection string
Example or connection string :

Data Source=.SQLEXPRESS;DataBase=PlaCliGen;User ID=XXX;password=ZZZ; Connect Timeout=30

,where PlaCliGen is the name (or logical name) by which SQLEXPRESS server knows the database.

By connecting to the data base with AttachDBFile giving the path to the .mdf file
(namely : replacing DataBase = PlacliGen by AttachDBFile = c:vsplacligenapp_dataplacligen.mdf) the File was connected exclusively and no other process could connect to the database.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x