【出坑】VS2012 连接LocalDB and SQL Server Express

最近在忙ASP.NET的大作业,使用的是VS2012,笔者图方便,想使用其内置的数据库,但是在连接字符串上遇到了不少的麻烦。折腾了很久,终于解决,鉴于网上都是基于VS2008的教程,实在太老,所以笔者把解决方案拿出来,也算是给看客指条路。

问题描述

在VS2012中,对连接字符串有一些新的更改,下面的英文是我在微软连接字符串的页面上截取的。

Converting Connection Strings between LocalDB and SQL Server Express

How to Convert a SQL Server Express Connection String to LocalDB
Project templates for Visual Studio 2010 and Visual Web Developer 2010 Express create connection strings that specify SQL Server Express databases. To convert one of these connection strings to LocalDB, make the following changes:

  • Change “Data Source=.\SQLEXPRESS” to “Data Source=(LocalDB\v11.0)”.
    This change assumes that you installed LocalDB with the default instance name. For more information, see Data Source later in this topic.
  • Remove “User Instance=True” if it is present. Also remove the preceding or following semicolon (;).

How to Convert a LocalDB Connection String to SQL Server Express

Project templates for Visual Studio 2012 and Visual Studio Express 2012 for Web create connection strings that specify LocalDB databases. To convert one of these connection strings to SQL Server Express, make the following changes:

  • Change “Data Source=(LocalDB)\v11.0” to “Data Source=.\SQLEXPRESS”.
    This change assumes that you installed SQL Server Express with the default instance name. For more information, see Data Source later in this topic.
  • If the connection string contains AttachDBFileName, add at the end “;User Instance=True”. Omit the semicolon (;) if the connection string already ends with one.

可以看出其中VS2012相对于VS2010的变化:

  1. 如果是连接SQL Server Express databases,需要将连接字符串定义为:Data Source=.\SQLEXPRESS” to “Data Source=(LocalDB\v11.0)
  2. 如果是连接LocalDB databases,需要将连接字符串定义为:Data Source=(LocalDB)\v11.0″ to “Data Source=.\SQLEXPRESS

Data Source的值有自己的定义,同时对User Instance的使用进行了规范:

  1. 在SQL Server Express databases需要将其移除,LocalDB实例都是“用户实例”,并且不需要在连接字符串中指定User Instance = true。
  2. 在LocalDB databases中,则需要;User Instance=True 这里要注意,把最后的”;”移除。

连接字符串

官网给的列表

《【出坑】VS2012 连接LocalDB and SQL Server Express》 列表部分截图

总结:

在百度上找不多类似的问题的解答,还是得靠谷歌

    原文作者:朱萧默说
    原文地址: https://www.jianshu.com/p/bb04e60e0ebf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞