Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C# 2008, Windows Forms, Access 2010 database.
I have a form that collects data and then saves to a directory on my computer..C:\\ drive. I am going to eventually have this application installed on different machines and have the data saved to a directory on our server. This directory may be set as the X drive on one machine and the Z drive on another. How do I do this? my current connection is written as so:
C#
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Temp\\Test.accdb");
Posted
Updated 18-Mar-13 10:20am
v2

The simplest way would be having the database path as a configurable item in your application and refer that value to build your connection string.
If the config item is not available use a default path defined by your application.

some samples
How to get Connection String from App.Config in c#[^]

provide a settings UI to modify this path by the users if they want to change this path later time.
 
Share this answer
 
v2
Comments
kp42 21-Mar-13 13:40pm    
How is the OleDBConnection to be written after following this sample? OleDbConnection myConnection = new OLleDbConnection(??); I tried using "connectionString", but I receiev an error that the ConnectionString property has not been initialized.
Did you ever think on using database with client-server architecture? And it even can be light-weight and not proprietary as Microsoft Access is. You see, file-based (or fileserver-based) Access will lead you nowhere.
This is the first problem you can see, further problems may haunt you badly.

Please see:
http://en.wikipedia.org/wiki/Microsoft_Access[^],
http://en.wikipedia.org/wiki/Client-server[^].

—SA
 
Share this answer
 
v2
Comments
kp42 21-Mar-13 13:42pm    
Thanks for the links, but how would you go about doing this?
Sergey Alexandrovich Kryukov 21-Mar-13 14:53pm    
My advice would be migrating to client-server. Consider MySQL, SQLLite, MS SQL Express, MS SQL, whatever. Please see:
http://en.wikipedia.org/wiki/List_of_relational_database_management_systems

—SA
Build your connection string at runtime.
Use GetEntryAssambly[^] and it's Location[^] property.

C#
using System;
using System.IO;
using System.Reflection;

namespace My
{
	class Program    
	{
		public static void Main()
		{
			string folder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
			string filename = Path.Combine(folder, "text.accdb");
			string connectiostring = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}",filename);
			
			Console.WriteLine(connectiostring);
		}
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900