0

My project is to extract some attributes from a folder with .mdb to .csv files. All functions are processing properly but I have an issue with the ConnectionString.

When run for the first time, it opens the connection for the first file in folder and extracts the .csv file and closes the connection. All good so far.

Afterwards, when it goes for the second file in the folder, it somehow opens again the ConnectionString for the first file but the function calls the second .mdb file. How can I make it open the second file?

static void Main()
{
    CreateFolder();

    string dst_fld = @"C:\csv\AllCsvFiles";
    string src_fld = @"C:\mdb";
    string dst_ext = ".mdb";

    string[] mdb_array = Directory.GetFiles(src_fld, "*" + dst_ext, SearchOption.TopDirectoryOnly); // Get all mdb files from a folder

    OleDbConnection cn = new OleDbConnection();

    foreach (string tname in mdb_array)
    {
        cn.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}",mdb_array);

        try
        {
            cn.Open(); // open the connection
            ExportFunction(cn, tname, dst_fld); // call function for export the csv file
        }
        finally
        {
            cn.Close();
        }
    }

    GetCSVFiles();
    DeleteFF();
}
4
  • What is the error message when cn.Open() executes for the second time? Commented Jun 21, 2016 at 10:27
  • The folder contains theses files: 27001DX.mdb 27002DX.mdb the error message: The Microsoft Jet database engine cannot find the input table or query '27002DX'. Make sure it exists and that its name is spelled correctly. Commented Jun 21, 2016 at 10:33
  • When run for the second time, using breakpoints, cn.ConnectionString "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\mdb\\27001DX.mdb" tname "C:\\mdb\\27002DX.mdb" string Commented Jun 21, 2016 at 10:36
  • 3
    ....0;Data Source={0}",mdb_array); instead of mdb_array, you want tname here i think? Commented Jun 21, 2016 at 10:44

1 Answer 1

1

As pointed by @steve16351, you need to change the connection string as follows :

cn.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", tname);
Sign up to request clarification or add additional context in comments.

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.