且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在不使用SQL Server的情况下访问.mdf数据库?

更新时间:2023-02-03 08:37:43

我认为不通过SQL Server引擎就无法与MDF文件进行交互。您很可能会安装SQL Server Express(如果选择通过Visual Studio的添加/删除程序,则应该选择该选项,也可以从此处单独下载-这是带有工具的2008 R2选项),附加数据库,然后从程序连接到该引擎。

I don't think it is possible to interact with an MDF file without going through a SQL Server engine. You would most likely install SQL Server Express (which should be an option if you choose to go through add/remove programs for Visual Studio, or you can download it separately from here - this is the 2008 R2 With Tools option), attach your database, and then connect to that engine from your program.

许多在线教程会建议您使用用户实例功能和 AttachDbFileName 。我建议您不要走那条路,因为这经常会造成混乱-许多很多用户通过Management Studio连接到数据库的一个实例,而不是他们程序中的另一个实例,然后不要了解为什么一个人看不到另一个人所做的更新。 用户实例功能已被弃用,我非常希望将数据库正确地附加到实例。

Many tutorials online will suggest you use the User Instances feature and AttachDbFileName. I suggest you don't go that route as it often leads to confusion - many, many users connect to one instance of the database through Management Studio, a different instance from their program, and then don't understand why one doesn't see the updates made by the other. The User Instances features is deprecated and I highly prefer attaching a database to the instance properly.

在SQL Server 2012中您可以下载新的 SqlLocalDb 运行时(您要从此处获取x86或x64 SqlLocalDB.MSI文件),这使此过程更加容易,因为它不是维护按需SQL Server引擎。不过请注意,您的数据库将升级为新的11.0文件格式,这意味着您将无法分离/附加或备份/还原到旧版本(2008、2008 R2等)。我添加了许多针对新用户的故障排除技术,从SqlLocalDb在这里

In SQL Server 2012 you can download the new SqlLocalDb runtime (you want either the x86 or x64 SqlLocalDB.MSI file from here), which makes this process much easier, as it is a no-maintenance on-demand SQL Server engine. However note that your database will be upgraded to the new 11.0 file format, which means you won't be able to detach/attach or backup/restore to an older version (2008, 2008 R2, etc). I added many troubleshooting techniques for new users starting with SqlLocalDb here.