AttachDbFilename 有什么问题 [英] what's the issue with AttachDbFilename

查看:31
本文介绍了AttachDbFilename 有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,在连接字符串中使用 AttachDbFilenameuser instance 是连接数据库的一种糟糕方式.我在我的本地机器上使用 SQL server express,它似乎一切正常.但是,连接到 SQL 服务器的正确方法是什么?

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?

感谢您的解释.

推荐答案

Using User Instance 意味着 SQL Server 正在创建该数据库文件的特殊副本以供您的程序使用.如果您有两个不同的程序使用相同的连接字符串,它们将获得两个完全不同的数据库副本.这会导致很多的混乱,因为人们会用他们的程序测试更新数据,然后在 Management Studio 中连接到他们数据库的不同副本,并抱怨他们的更新不起作用.这让他们经历了一系列有缺陷的疯狂追逐步骤,试图解决错误的问题.

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.

本文更深入地介绍如何使用此功能,但请注意第一个注意事项:User Instance 功能已被弃用.在 SQL Server 2012 中,首选的替代方案是(按此顺序,恕我直言):

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. 创建或附加您的数据库到 SQL Server 的真实实例.然后您的连接字符串只需要指定实例名称、数据库名称和凭据.不会有任何混淆,因为 Management Studio、Visual Studio 和您的程序都将连接到数据库的单个副本.

  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.

使用 SqlLocalDb 进行本地开发.我相信我昨天向您指出了这篇文章:SQL Server 2012 Express LocalDB 入门."

Use SqlLocalDb for local development. I believe I pointed you to this article yesterday: "Getting Started with SQL Server 2012 Express LocalDB."

当然,如果您使用的是 <SQL Server 2012,SqlLocalDb 不是一个选项 - 所以你应该创建一个真正的数据库并一致地使用它.为了完整性,我只提到 Compact 选项 - 我认为这几乎和使用 AttachDbFileName 一样糟糕.

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.

我已经在这里写了博客:

I've blogged about this here:

这篇关于AttachDbFilename 有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆