?
Solved

Connection Strings in my settings

Posted on 2014-10-16
2
Medium Priority
?
127 Views
Last Modified: 2014-10-20
Experts, how can i have a connection string in my settings that can do the following? The database for my application will be in appdata\Local so I cannot have a hard coded path since it will vary with every user.

    Public fPath As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\MyCompany\MyApp"
    Public strLocalConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fPath & "\MyApp.accdb"

Open in new window

0
Comment
Question by:Basicfarmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 40385867
There is an easy way to insert information that is available only in code into a string that comes from the resources or the settings.

Define your connection string with a {0} marker, as in the following:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}\MyApp.accdb"

When you need to use it in your code, do it the following way:

String.Format(My.Settings.YourConnectionString,Environment.GetFolderPath,(Environment.SpecialFolder.LocalApplicationData) & "\MyCompany\MyApp")

Open in new window


String.Format will replace the {0} in the string with the second parameter. If you have more than one string to insert, simply add more parameters and define markers {0}, {1}, {2}... to indicate which parameter goes where.
0
 

Author Closing Comment

by:Basicfarmer
ID: 40392017
That is exactly what I needed, Thanks James...
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question