首页 > 解决方案 > Connection string is not working while using (data source =.\SQLExpress)

问题描述

I'm using ASP.NET with C# and SQL Server

T have to use my .mdf file inside the App_Data folder because I don't have access to the SQL database.

T have a problem with this connection string only if T use .\SQLExpress

<connectionStrings>
        <add name="msscEduConnectionString" 
             connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MsscEdu.mdf;Integrated Security=True;" 
             providerName="System.Data.SqlClient"/>
      </connectionStrings>

This connection string is not working and it shows error with attaching database

We have another site with connection string like this but different name and it works fine.

This is the connection string for the other site

<connectionStrings>

  <add name="msdschoolkjConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\msdschoolkj.mdf;Integrated Security=True;User Instance=True"
   providerName="System.Data.SqlClient" />
 </connectionStrings>

If I use the following connection string it works only on my computer but not the server

<connectionStrings>
    <add name="msscEduConnectionString" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MsscEdu.mdf;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

Can anyone assist?

标签: c#sqlasp.netsql-serverconnection

解决方案


在你的 web.config 文件中试试这个

<connectionStrings>
  <add name="ConnectionName"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>

推荐阅读