I've been working in C# for the first time these past few days and while I'm getting most of it, there's a few quirks i'm not quite sure about.

My current problem is I am developing a program that needs a database, but because I'm going to be moving it from place to place, my current scheme of using an absolute filename for the database won't work in the future.

static OleDbConnection dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; 
            Data Source=C:\\Documents and Settings\\darrenb\\My Documents\\Visual Studio 2008\\Projects\\FileGrabber\\FileGrabber\\bin\\Release\\files.mdb");

Is there some way I can reference the database more locally, like one would do for a website? If so, where would I have to put the database?

Recommended Answers

All 7 Replies

Put this connection string in .config file, and replace it whenever you need wherever database in.

Thanks for your reply. I don't understand how that is much better though. It looks like that just kind of shifts where I need to replace the file name every time I move it. I took another look at it and in the database's current location (the same as the .exe for the program) I can just use "files.mdb" as the data source string

My solution is to keep your database location isolated from source code, wherever your database moved to, you just will open the .config file edit the connection string in your case (@"Provider=Microsoft.Jet.OLEDB.4.0; .....) without modifying the program code (C# code)!
Now is it understood?!!

I understand where you are coming from now. I realize I may have been unclear in what I meant. Right now I'm writing the program on a company laptop. Once it's done and running on the server, the database (and the rest of the project) won't be at the absolute path that it was on my computer, but it will still be the same structure within what I've written so i wanted to know how to reference the database without the absolute path.

I've never messed with a config file in my limited experience, so thanks for the tip on that. One more question though. If the connection info is in that config file, how should I instantiate the connection from within the c# code? No parameters in the constructor method call?

Dear friend!!! you don't read what I wrote well! that's cost me to write the same thing more over!
I know your problem and my previous answers would solve it.
Create .setting file and add ConnectionString with proper value..

static OleDbConnection dbConnection = new OleDbConnection(Properties.Settings.Default.ConnectionString);

So, what you need to deploy it on company machine is to change the value of ConnectionString in the .setting file

If it solved your problem, please mark the thread as solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.