【问题标题】:sql local database connectionsql本地数据库连接
【发布时间】:2014-11-15 09:17:12
【问题描述】:

我正在尝试使用以下代码在 c# 中打开我的本地数据库:

SqlConnection c = new SqlConnection("Server = (LocalDB)\\v11.0 ;Integrated Security=True;Database =Informati;");
c.Open();

但是当他试图打开它时我收到一个错误:

Cannot open database "Informati" requested by the login. The login failed.
Login failed for user 'AURELIAN121\Aurelian'.

我尝试使用

进行连接

new SqlConnection("Server = (LocalDB)\v11.0 ;User id=AURELIAN121\Aurelian;Integrated Security=True;Database =Informati;");

但错误仍然存​​在。

【问题讨论】:

标签: c# sql-server database local-database


【解决方案1】:

您的连接字符串不正确: 对于本地数据库,你也可以使用点,比如

SqlConnection c = new SqlConnection("Data Source=.;
Integrated Security=True;Initial Catalog=Informati;");

从错误中可以看出,您的本地用户在 Sql 服务器上没有访问权限。你必须先添加创建登录:http://www.reliasoft.com/support/rs40024.htm

【讨论】:

    【解决方案2】:

    这对我有用

    1. Data Source=(LocalDb)\v11.0;Initial Catalog=CSN;Integrated Security=SSPI;
    

    完整版

    2. <add name="DefaultConnection" 
       connectionString="Data Source=(LocalDb)\v11.0;
       Initial Catalog={catalog name};
       Integrated Security=SSPI;
       AttachDBFilename=|DataDirectory|\{database name}.mdf"        
       providerName="System.Data.SqlClient" />;
    

    其中 {catalog name} 是您的数据库名称,{database name} 也是您的数据库名称

    在这里您可以找到连接字符串的完整列表

    http://www.connectionstrings.com/sql-server-2012/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 2018-02-04
      相关资源
      最近更新 更多