0

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?

Dale K
  • 25,246
  • 15
  • 42
  • 71
progtariq
  • 43
  • 6
  • Why not just use localdb if that works? Do you have SQLExpress installed? You shouldn't count on using the same connection string between dev and production so just plan on transforming that setting during deployment. – Crowcoder Jan 26 '20 at 16:01
  • as i wrote local only working on my computer not the server. – progtariq Jan 26 '20 at 16:13
  • I know. You won't be using localdb in production but that's OK. You generally change the connection string to point to production during deployment. – Crowcoder Jan 26 '20 at 16:24
  • have you tried hard coding the path to DB files? instead of |DataDirectory|? – Jawad Jan 26 '20 at 16:42
  • the problem is with the .\sqlexpress only even on my computer not working and the .\sqlexpress is working fine with the other website we have and its installed – progtariq Jan 26 '20 at 16:48
  • if .\sqlexpress works on my computer it will work a the server also – progtariq Jan 26 '20 at 16:50
  • for a user instance: User Instance=True; in the connection string(by default it is false) – lptr Jan 26 '20 at 17:11

1 Answers1

0

Try this in your web.config file

<connectionStrings>
  <add name="ConnectionName"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>
  • cannot be opened because it is version 852. This server supports version 663 and earlier. – progtariq Jan 26 '20 at 17:43
  • Refer this URL found on might be helpful [link](https://stackoverflow.com/questions/46431043/cannot-be-opened-because-it-is-version-852-this-server-supports-version-782-and) – Akshay Phadke Jan 26 '20 at 17:51
  • i recreated the database from another computer on SQL server 2008 r2 and add the user instance and it works fine thank you – progtariq Jan 26 '20 at 18:18