Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

c# - How to add SQL Server database file (.mdf) in Visual Studio without installing SQL Server Express Edition?

I have an error below adding an .mdf file (SQL Server Database) in a Visual Studio 2010 project

Connections to SQL Server database files (.mdf) require SQL Server 2005 Express or SQL Server 2008 Express to be installed and running on the local computer

I don't want to install SQL Server Express (2005/2008) because I have already installed SQL Server 2005 Enterprise Edition

I am using Visual Studio 2010 Ultimate

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a really annoying one. Basically, in Machine.config for the version of the framework you are developing against, there is an entry for LocalSqlServer.

On my machine, for version 4:

C:WindowsMicrosoft.NETFrameworkv4.0.30319ConfigMachine.config

<add name="LocalSqlServer" connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />

I found that if I changed the data source part of the connection string to point at my Sql 2005 full server instance, then the error you mentioned went away.

(Similar for other versions of the framework, which I also changed)

I can't remember if I needed to restart just visual studio or the whole machine before I saw the changes work.

Remember to back up your machine.config files before editing them!

With that being said, there's also no reason why you can't add the database into Sql Server itself (if you have the mdf) then connect to it from Visual Studio via the View -> Server Explorer -> Data Connections (Right Click -> Add Connection) - have you tried that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...