what's the issue with AttachDbFilename

后端 未结 2 1683
抹茶落季
抹茶落季 2020-12-01 21:53

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

相关标签:
2条回答
  • 2020-12-01 22:40

    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 SqlLocalDb for local development. I believe I pointed you to this article yesterday: "Getting Started with SQL Server 2012 Express LocalDB."

    3. 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:

    • Bad Habits : Using AttachDBFileName
    0 讨论(0)
  • 2020-12-01 22:41

    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:\vs\placligen\app_data\placligen.mdf) the File was connected exclusively and no other process could connect to the database.

    0 讨论(0)
提交回复
热议问题