error occurred while establishing a connection to SQL Server

前端 未结 8 1287
谎友^
谎友^ 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: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");
    

提交回复
热议问题