Solved

Connection Strings in my settings

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

19 Experts available now in Live!

Get 1:1 Help Now