Solved

Connection Strings in my settings

Posted on 2014-10-16
2
114 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
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now