简体   繁体   English

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

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

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. 此数据库文件与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 有关详细信息,请参阅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? 其他:为什么Visual Studio在这些年后没有实现单击错误对话框中的链接? Is it really hard to implement? 实施起来真的很难吗?

my simple way: 我的简单方法:

  1. go to VisualStadio 2012 转到VisualStadio 2012
  2. In Server Explorer Windows Server Explorer Windows中
  3. open Data Connections 打开数据连接
  4. Right click on ASPNETDB.MDF 右键单击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 完成(快乐):D

From my experience of a few instants ago: 根据我几个月前的经历:

  1. Forget about trying to open the old SQL Server Express .mdf file 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. 忘记尝试在Visual Studio 2012中打开旧的SQL Server Express .mdf文件。在VS 2010的旧VS 2010解决方案的转换过程中,它告诉您可以通过双击数据库文件并将其升级为使用LocalDb来实现代替。 That's a complete lie! 这是一个完全的谎言! :D :d

  2. Open SQL Server Management Studio , select the Databases node, right click it and select Attach... . 打开SQL Server Management Studio ,选择“ Databases节点,右键单击它并选择“ Attach...

  3. Provide the path for the .mdf file, click OK and then you should be good to go. 提供.mdf文件的路径,单击“确定”然后您应该好好去。

  4. Now inside Visual Studio 2012 , open Server Explorer (menu View -> Server Explorer ), right click Data Connections and then select Add Connection... . 现在在Visual Studio 2012 ,打开Server Explorer (菜单View -> Server Explorer ),右键单击Data Connections ,然后选择Add Connection... In Server name: put (localdb)\\v11.0 and in Connect to a database: select the database you attached previously. Server name: put (localdb)\\v11.0并在“ Connect to a database:选择先前附加的数据库。

  5. Right click the Data Connection just added in Solution Explorer and select Properties . 右键单击Data Connection只是添加Solution Explorer ,选择Properties Copy the Connection String property and replace the Web.Config or App.Config one with this value. 复制Connection String属性并使用此值替换Web.ConfigApp.Config

Note: In SQL Server Management Studio I got an error while trying to attach an old .mdf file I got from this sample project from DayPilot: Scheduler for ASP.NET MVC 4 Razor (C#, VB.NET, SQL Server) . 注意:SQL Server Management Studio我尝试附加一个旧的.mdf文件时遇到错误,该文件来自DayPilot的示例项目: ASP.NET MVC 4 Razor的调度程序(C#,VB.NET,SQL Server) It was related to a path issue regarding the .log file that accompanies the .mdf database file. 它与.mdf数据库文件附带的.log文件的路径问题有关。 I just selected the .log file entry in the Attach... dialog window and removed it. 我刚刚在Attach...对话框窗口中选择了.log文件条目并将其删除。 Then tried to attach the database again and it worked. 然后尝试再次附加数据库,它工作。

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

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

open sql server management studio connect localDB remove deattach same name mdf file then attach your mdf file 打开sql server管理工作室连接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-> dataconnection右键单击数据库 - >修改连接 - >文本连接 - >单击确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM