Problem accessing localDB when moving application to new folder

Ronny Mees 20 Reputation points
2023-07-15T16:24:29.39+00:00

In a CRUD class I have declared the following connection string:

private static string dbConnection = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + System.AppDomain.CurrentDomain.BaseDirectory + "\\PlexSeries.mdf;Integrated Security=True;Connect Timeout=30";

And the following GET method:

public DataTable Get()
{

 DataSet dataset = new DataSet();

 using (SqlConnection connection = new SqlConnection(dbConnection))
 {
     try
     {
          SqlDataAdapter adapter = new SqlDataAdapter();
          adapter.SelectCommand = new SqlCommand("SELECT * FROM dbo.Series", connection);
          adapter.Fill(dataset);
          return dataset.Tables["Table"];
     }
     catch (Exception ex)
     {
         MsgBox.Show(ex.Message, "Error", MsgBox.Buttons.OK, MsgBox.Icon.Error, MsgBox.AnimateStyle.FadeIn);
         return null;
     }
 }

}

Upon getting to the line adapter.Fill(dataset), I get the following error:

But when i copy the debug folder to an other location...

Upon getting to the line adapter.Fill(dataset), I get the following error:

Error Image

I tried to solve this by using a relative path to the database file but that didn't solve the problem.

In the previous version of this solution this was not a problem but that was build with Visual Studio 2019 and now I am using Visual Studio 2022.

Any tips on how to solve this?

SQL Server Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Raksha Chourasia 75 Reputation points
    2023-07-17T06:49:08.11+00:00

    The error that you have received shows that the connection string is not able to locate the database file when you are running the application from a different location.

    This error is caused because of the path for the database file which is relative to the current directory.

    For resolving this you can modify the connected string to an absolute path for the database file

    You can use this type of code to set it:

    private static string dbConnection = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\path\to\your\database\PlexSeries.mdf;Integrated Security=True;Connect Timeout=30";

    Also, Replace C:\path\to\your\database with the actual path to the PlexSeries.mdf file on the system.

    As well you can use an alternative method for the correct path:

    private static string dbConnection = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PlexSeries.mdf") + ";Integrated Security=True;Connect Timeout=30";

    Also, have a look to match the correct path with PlexSeries.mdf file.

    This will help you in establishing the connection.

    I hope this will help!!


1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-07-15T21:27:37.98+00:00

    So why do you use AttachDBFilename in the connection string in the first place? This is a very odd parameter, that I have never understood the point with. The normal connection-string keyword to specify the database is Database or Initial Catalog. (The two are equivalent.)

    There error you get is exactly because you dabble with AttachDBFilename.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.