Is it possible to access a .mdf database without SQL Server?

后端 未结 3 874
孤街浪徒
孤街浪徒 2020-12-01 18:06

I created a .mdf file by SQL Server and put it to a local file. I run my program in a computer which has no SQL Server installed. And also I used classes from t

3条回答
  •  有刺的猬
    2020-12-01 19:01

    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.

    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.

    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.

提交回复
热议问题