如何在 VS 2012 中使用 LocalDb 连接到旧的 sql 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15417483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:16:16  来源:igfitidea点击:

How do I connect to an older sql database using LocalDb in VS 2012?

sqlsql-servervisual-studio-2012sql-server-expresslocaldb

提问by ATL_DEV

I am trying to construct a connection string to a 2008 SQL Express database file in VS 2012. I am getting an error dialog:

我正在尝试在 VS 2012 中构建到 2008 SQL Express 数据库文件的连接字符串。我收到一个错误对话框:

This database file is not compatible with the current instance of SQL Server. To resolve this issue, you must upgrade the database file by creating a new data connection, or you must modify the existing connection to this database file. For more information, see http://go.microsoft.com/fwlink/?LinkId=235986

此数据库文件与 SQL Server 的当前实例不兼容。要解决此问题,您必须通过创建新数据连接来升级数据库文件,或者必须修改与此数据库文件的现有连接。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=235986

I followed the link to Microsoft's always wonderfully helpful documentation (cough, cough..). When I went through the upgrade database function, I was not presented with a prompt to upgrade the file. I was really hoping to use LocalDb instead of having to do a install of SQL Express 2008. Any thoughts?

我跟着链接到微软总是非常有用的文档(咳嗽,咳嗽......)。当我完成升级数据库功能时,我没有看到升级文件的提示。我真的希望使用 LocalDb 而不是必须安装 SQL Express 2008。有什么想法吗?

OTHER:Why hasn't Visual Studio implemented clicking on links in the error dialogs after all these years? Is it really hard to implement?

其他:为什么 Visual Studio 这么多年没有实现点击错误对话框中的链接?执行起来真的很难吗?

回答by OmiD

my simple way:

我的简单方法:

  1. go to VisualStadio 2012
  2. In Server ExplorerWindows
  3. open Data Connections
  4. Right click on ASPNETDB.MDF
  5. click on Modify Connection...
  6. a new windows open for you ... you just click OK
  7. if other windows open for you click Yes
  8. FINISH ( be Happy ) :D
  1. 前往VisualStadio 2012
  2. 服务器资源管理器窗口中
  3. 打开数据连接
  4. 右键单击ASPNETDB.MDF
  5. 单击修改连接...
  6. 一个新窗口为您打开……您只需单击“确定”
  7. 如果其他窗口为您打开,请单击
  8. 完成(快乐):D

回答by Leniel Maccaferri

From my experience of a few instants ago:

根据我前几天的经验:

  1. Forget about trying to open the old SQL Server Express.mdffile in Visual Studio 2012. During the conversion process of an old VS 2010 solution to VS 2012 it tells you can do that by just double clicking the database file and upgrade it to use LocalDb instead. That's a complete lie! :D

  2. Open SQL Server Management Studio, select the Databasesnode, right click it and select Attach....

  3. Provide the path for the .mdffile, click OK and then you should be good to go.

  4. Now inside Visual Studio 2012, open Server Explorer(menu View -> Server Explorer), right click Data Connectionsand then select Add Connection.... In Server name:put (localdb)\v11.0and in Connect to a database:select the database you attached previously.

  5. Right click the Data Connectionjust added in Solution Explorerand select Properties. Copy the Connection Stringproperty and replace the Web.Configor App.Configone with this value.

  1. 忘记尝试SQL Server Express.mdf在 Visual Studio 2012 中打开旧文件。在将旧 VS 2010 解决方案转换为 VS 2012 的过程中,它告诉您只需双击数据库文件并将其升级为使用 LocalDb 即可。那是彻头彻尾的谎言!:D

  2. 打开SQL Server Management Studio,选择Databases节点,右键单击它并选择Attach...

  3. 提供.mdf文件的路径,单击“确定”,然后您就可以开始使用了。

  4. 现在在里面Visual Studio 2012,打开Server Explorer(菜单View -> Server Explorer),右键单击Data Connections然后选择Add Connection...。在Server name:put(localdb)\v11.0和 in 中Connect to a database:选择您之前附加的数据库。

  5. 右键单击Data Connection刚刚添加的Solution Explorer并选择Properties。复制该Connection String属性并将Web.Config或替换App.Config为该值。

Note:In SQL Server Management StudioI got an error while trying to attach an old .mdffile I got from this sample project from DayPilot: Scheduler for ASP.NET MVC 4 Razor (C#, VB.NET, SQL Server). It was related to a path issue regarding the .logfile that accompanies the .mdfdatabase file. I just selected the .logfile entry in the Attach...dialog window and removed it. Then tried to attach the database again and it worked.

注意:SQL Server Management Studio尝试附加.mdf从 DayPilot: Scheduler for ASP.NET MVC 4 Razor (C#, VB.NET, SQL Server) 的示例项目中获得的旧文件时,我遇到了错误。它与有关数据库文件.log随附文件的路径问题有关.mdf。我刚刚.logAttach...对话框窗口中选择了文件条目并将其删除。然后尝试再次附加数据库并且它起作用了。

回答by Jogai

You can just do:

你可以这样做:

USE [master]
GO
CREATE DATABASE [database_name] ON 
    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\<database name>.mdf' ),
    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\<database name>.ldf' )
FOR ATTACH ;
GO

As described here: http://technet.microsoft.com/en-us/library/ms165673%28v=sql.105%29.aspx

如此处所述:http: //technet.microsoft.com/en-us/library/ms165673%28v=sql.105%29.aspx

回答by Owaix Ansari

go to VisualStadio 2012 In Server Explorer Windows open Data Connections Right click on ASPNETDB.MDF click on Modify Connection... a new windows open for you ... you just click OK if other windows open for you click Yes

转到 VisualStadio 2012 在服务器资源管理器中 Windows 打开数据连接右键单击 ASPNETDB.MDF 单击修改连接...为您打开一个新窗口...如果其他窗口为您打开,您只需单击确定单击是

回答by hooman_b

open sql server management studio connect localDB remove deattach same name mdf file then attach your mdf file

打开sql server management studio连接localDB删除deattach同名mdf文件然后附加你的mdf文件

go to visual studio in server explorer-> dataconnection right click on your database -> modify connection -> text connection -> click OK

转到服务器资源管理器中的visual studio-> 数据连接右键单击您的数据库-> 修改连接-> 文本连接-> 单击确定