Solved

Connection Strings in my settings

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
in Dot net,lastest version of MVC 3 23
VB: Convert 2 dates to specific format 24 48
Datagridview column when exported to Excel changes vb.net 1 13
get row value in vb.net 4 14
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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 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