error occurred while establishing a connection to SQL Server

前端 未结 8 1244
谎友^
谎友^ 2020-12-18 21:35

If I have my connection string in the web.config like this (added line feeds for better readability):



        
相关标签:
8条回答
  • 2020-12-18 22:03

    since this is web site project you need to specify correct .net framework version to build. SqlLocalDb need .net framework 4.

    if you create web project and add web site source files to it, it will work if the .net framework is 4+. When you open web site default target framework or existing dll framework may not be same and may be .net 3.5, that's why you get this error. hope this helps.

    enter image description here

    0 讨论(0)
  • 2020-12-18 22:04

    The problem was related to the application not running in .net version 4.0

    According to the following page: http://www.connectionstrings.com/sql-server-2012#sqlconnection

    You need .net 4.0 and up to use named pipes:

    The Server=(localdb) syntax is not supported by .NET framework versions before 4.0.2. However the named pipes connection will work to connect pre 4.0.2 applications to LocalDB instances.

    SqlLocalDB.exe create MyInstance
    SqlLocalDB.exe start MyInstance
    SqlLocalDB.exe info MyInstance
    

    The info provides the named pipe then this can be used in the connection string:

    <add name="conn" 
    providerName="System.Data.SqlClient" 
    connectionString="Server=np:\\.\pipe\LOCALDB#B1704E69\tsql\query"/>
    
    0 讨论(0)
  • 2020-12-18 22:05

    Also... this could be related to your setup in IIS.

    Your website's (session state) could be setup to use SQL Server for your session. Check there too... I just had this issue, and realized our sqlbox connection changed and forgot to update on IIS.

    0 讨论(0)
  • 2020-12-18 22:08

    Instead of using (localdb), have you tried using '.\SQLExpress;' per this page on MSDN? It uses the default instance, which may not be what you need.

    it would read like so:

    <add 
    name="conn" 
    connectionString="Provider=SqlClient;Data Source=.\\SQLExpress; Initial 
        Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True; 
        AttachDbFilename=D:\Products.mdf" 
    providerName="System.Data.SqlClient"/>
    
    0 讨论(0)
  • 2020-12-18 22:09

    Check the version of the reference System.Data between the empty project that works, and your project that does not work. You can use the solution explorer to check this. Are both projects using the same version?

    enter image description here

    0 讨论(0)
  • 2020-12-18 22:12

    Just for chuckles instead of putting (localdb) put a period '.'. So that it would look like

    Data Source=.\v11.0;
    

    I'd be surprised if it works but you can do that in full SQL.

    0 讨论(0)
提交回复
热议问题