Solved

Connection Strings in my settings

Posted on 2014-10-16
2
119 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

829 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