0

I have been using a static app.config connection string for my program with no issues until I realised that if I want to share my project with the professor the connection to the database is no longer there when moving the program to a new computer.

"Data Source=DESKTOP-etcetc;Initial Catalog=School;Integrated Security=True"

I have been browsing all over the web for a relative connection string that works for me, but most of them don't even work on the main pc..

I have saved the database file to School.mdf and I am trying to get the program to use this db file from the debug/exe folder, can someone help me with this?

Update

This is the error I am getting now

System.Data.SqlClient.SqlException: 'The database 'C:\USERS\EMIGH\SOURCE\REPOS\SWIMMINGSCHOOL\SWIMMINGSCHOOL\BIN\DEBUG\DATABASE\SCHOOLL.MDF' cannot be opened because it is version 904. This server supports version 852 and earlier. A downgrade path is not supported. Could not open new database 'C:\USERS\EMIGH\SOURCE\REPOS\SWIMMINGSCHOOL\SWIMMINGSCHOOL\BIN\DEBUG\DATABASE\SCHOOLL.MDF'. CREATE DATABASE is aborted.

An attempt to attach an auto-named database for file C:\Users\emigh\source\repos\SwimmingSchool\SwimmingSchool\bin\Debug\Database\Schooll.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'

Below is the path for the DataDirectory:

C:\Users\emigh\source\repos\SwimmingSchool\SwimmingSchool\bin\Debug

\Database

Is there any other workaround I could try to get this to work on other computers?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Friday
  • 13
  • 3
  • Hello@ChetanRanpariya that does help a bit, but after doing exactly as you explained my connection string looks like this SqlConnection conn = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\School.mdf;Integrated Security=True"); and I still cannot select from the database :( – Friday Jul 05 '21 at 02:55
  • What value you set for `DataDirectory` ? Do you copy the mdf file along with the other application files to the target computer? Does the target computer run the SQL server? Can you update the question with the latest details of the issue? – Chetan Jul 05 '21 at 03:52
  • Try this there are many options/answers available here https://stackoverflow.com/questions/12566036/an-attempt-to-attach-an-auto-named-database-for-file-database1-mdf-failed – Chinmay T Jul 05 '21 at 03:59
  • 1
    `cannot be opened because it is version 904. This server supports version 852 and earlier` looks like the SQL server you are trying to connect to has different version then the one which created the MDF file. Check answer of https://stackoverflow.com/questions/60691154/the-database-cannot-be-opened-because-it-is-version-904-this-server-supports-ve – Chetan Jul 05 '21 at 04:01
  • Also I see `Scholl.mdf` and `School.mdf` mentioned in the question... is this a typo? did you check the database file name in the code and in the directory? You have path `C:\Users\emigh\source\repos\SwimmingSchool\SwimmingSchool\bin\Debug\Database` on the new computer also? – Chetan Jul 05 '21 at 04:02
  • yes, that was a typo, fixed it now, still getting both errors, will check further on the link you provided, thank you! – Friday Jul 05 '21 at 04:08
  • i am upgrading the server version now, i haven't yet tried to run the program on another pc since it doesn't work on the main one, i am using the method @ChetanRanpariya posted in the first link to change the path of DataDirectory, so the connstrng should always return the correct location of the db file – Friday Jul 05 '21 at 04:21

0 Answers0