0

I'm trying to build a program in C# (visual studio) that has a Database, but it has to go to specific folder that the user picks when running the program for the first time. The link to the folder is stored in a text file. This is all done using a windows form. I want the app.config file to read the text file so it knows where the database is located. My app.config file:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>    
    <add name="MyProgram.Properties.Settings.DataBaseTestingConnectionString"
      connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\MyProgram\DataBaseTesting.accdb"
      providerName="System.Data.OleDb" />       
    <add name="cn" connectionString="data source=.;initial catalog=NORTHWIND;user id=sa" 
         providerName="System.Data.OleDb"  />    
  </connectionStrings>
</configuration>

How can i make source be first line with link in text file (location.txt) located in Desktop? instead of E:\MyProgram\DataBaseTesting.accdb

----SOLUTION-17/06/2022--------------------------------------------------------------------------------- Finally figured it out. First had to change App.Config file and change Data Source. Instead of:Data Source=E:\MyProgram\DataBaseTesting.accdb" i had to change it to: Data Source=|DataDirectory|\DataBaseTesting.accdb" Seconddly in c# file i placed this line: AppDomain.CurrentDomain.SetData("DataDirectory", Global.path); Global.path is my global variable that stored the folder location of my database.

  • A possible solution is [explained here](https://stackoverflow.com/questions/480538/use-xml-includes-or-config-references-in-app-config-to-include-other-config-file) or you can just use the [OleDbConnectionStringBuilder](https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbconnectionstringbuilder?view=dotnet-plat-ext-6.0) class programmatically and configure your connection string reading the other external text file – Steve Jun 11 '22 at 21:14
  • Thanks for fast reply. I've read the links you gave, but i still dont understand how to programatically make app.config file read the text file i created that contains link to folder where database is stored. If it isnt asking too much can you write code to do it. Sorry i'm a true noob at most of these stuff. – Nelson Furtado Jun 11 '22 at 21:33
  • Or would it be better to have a Global variable in C# window form store location of folder where is database and in the app.config file have something like Source= GlobalVariable.dataBase1 + "\DataBaseTesting.accdb" – Nelson Furtado Jun 11 '22 at 21:38
  • This is what i've done so far but not working... Really need some help. – Nelson Furtado Jun 12 '22 at 15:19
  • OleDbConnection conectSring1; conectSring1 = new OleDbConnection(); conectSring1.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Global.linkFolderDB + "\\Database1.accdb; Persist Security Info=False;"; InitializeComponent(); – Nelson Furtado Jun 12 '22 at 15:19
  • After you change the string in that way, do you create and reopen the connection using the variable _conectString1_? – Steve Jun 12 '22 at 15:30
  • Not sure. How do i do that? – Nelson Furtado Jun 12 '22 at 20:25

0 Answers0