I'd like my program to reference a relative path to the .mdf
file so it can run on a new machine without modifying the connection string.
Environment:
- Microsoft Visual Studio Community 2019, Version 16.11.8
- Windows Forms
The connection string is pulled from the dbstring
class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sharpening_Teeth
{
public class dbString
{
// modifyTemplate pulls in the connection string from this class
public static string getConnectionString()
{
return "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\vamel\\OneDrive\\Desktop\\test\\Sharpening Teeth\\Sharpening Teeth\\templateDB.mdf";
}
public static string getReadQuery()
{
return "SELECT template_id, template_name, message_content FROM dbo.message_template";
}
}
}
I'm just getting back into programming and I'm stuck. Hopefully someone can point me in the right direction here. Thank you.
Threads reviewed
How to set a relative path to a SQL Server Compact database?
SQL Express connection string: mdf file location relative to application location
connection string with relative path to database not working?
How to set a relative path to a SQL Server Compact database?
C# Visual Studio: |DataDirectory| keyword in connection string conflicts mdf files
EDIT
I found a solution on reddit. https://www.reddit.com/r/csharp/comments/k0xaqb/local_db_sql_connection_string_without_using_the/