1

i am planning to make my data source to auto detect the file path of my .mdf file and my current code is this

 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    con.ConnectionString = "Data Source= 
(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\xxeno\source\repos\Keyboard Part Picker 
Layout(2)\keyboardpartpickerDB.mdf;Integrated Security=True"
    If con.State = ConnectionState.Open Then
        con.Close()

    End If

    con.Open()
    disp_data()

End Sub

i any solution so that it "auto-detects the attach db filename?

1 Answers1

0

That's a very bad location for your data file in the first place. You are referring to the file in the project folder and that's bad. The way this should work is that you place the source data file in the project folder and then, when you build, that file is copied to the output folder. It is that copy that your application uses when it runs. That means that you can make as much of a mess of the copy as you want while testing as you will always have the original source file to go back to. When you want to reset your test data file, you simply create a new copy and overwrite the existing one. When you're ready to deploy your app, a copy of the clean source file will be created in the Release output folder and you're good to go. If you do it that way then you can simply use "|DataDirectory|" for the folder path in your connection string and it will just work:

con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\keyboardpartpickerDB.mdf;Integrated Security=True"