0

I have a line of code that builds the connection string to an Access database.

private static string ConnectionString = 
            @"Provider=Microsoft.Jet.OLEDB.4.0;" + 
            @"Data source= C:\Documents and Settings\username\My Documents\AccessFile.mdb";

How can I use any of these bits of code: Path.GetDirectoryName() or System.Environment.CurrentDirectory?

System.IO.Path.GetDirectoryName(System.Environment.CurrentDirectory)

2 Answers 2

4

It's not clear where in the string you'd like to insert this, but I'll assume you want to replace the entire directory path. To do this, you could just concatenate the strings together using +:

private static string ConnectionString = 
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + 
    System.IO.Path.GetDirectoryName(System.Environment.CurrentDirectory) +
    @"\AccessFile.mdb";

Alternatively (and probably better) would be to use string.Format:

private static string ConnectionString = string.Format(
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}\AccessFile.mdb", 
    System.IO.Path.GetDirectoryName(System.Environment.CurrentDirectory));
Sign up to request clarification or add additional context in comments.

1 Comment

i vey surprised for the quick answer i going to try it soon
0

Try Using

using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.OleDb;

namespace DAL 
{
    public class OLEDBhelper
    {
        private static string ConnectionString = 
            @"Provider=Microsoft.Jet.OLEDB.4.0;" + 
            @"Data source= " + System.IO.Path.GetDirectoryName(System.Environment.CurrentDirectory) + "\AccessFile.mdb";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.