error occurred while establishing a connection to SQL Server

前端 未结 8 1245
谎友^
谎友^ 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:28

    This error showed up after I compressed the C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA directory. Uncompressing it fixed the error.

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

    You've properly escaped the db filename but not the datasource, therefore it tries to connect to a datasource named "(localdb)11.0", which (most likely) doesn't exist.

    Try escaping it properly like this:

    SqlConnection cn = new SqlConnection("Data Source=(localdb)\\v11.0;"+
    "Initial Catalog=MyDB; Integrated Security=True; "+
    "MultipleActiveResultSets=True; AttachDbFilename=D:\\Products.mdf");
    
    0 讨论(0)
提交回复
热议问题