Using C#, how do I assign a value to the connection string in my code, particulary the value for Data Source?

Posted on 2012-08-12
Last Modified: 2012-08-13
I am developing a C# Windows platform application using VS 2005.

Here is a snippet of my code:

the value for the 1st parameter "files" is: "U:\\Visual Studio 2005\\Projects\\UDLApplication\\UDLApplication\\bin\\Debug\\ReceivedFall\\106.XLS"

How would I assign the value of "files" to "Data Source" in the following string titled sourceConnString ?

The problem is that presently I get the following value for the string sourceConnString including Data Source, which is obviously wrong:
"Provider=Microsoft.Jet.OLEDB.4.0;  \r\n\r\n                       Data Source = @files;\r\n\r\n                       Extended Properties=Excel 8.0"

        private void ProcessXLStoSQLTable(string files, string folder, string fileName)

            string sourceConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;  

                       Data Source = @files;

                       Extended Properties=Excel 8.0";

            DataTable sourceData = new DataTable();
Question by:zimmer9
    LVL 53

    Expert Comment

    Why don't you use the following

    string pathAndFileName = folder + "\" + filename;
    string sourceConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;  " + pathAndFileName  + "Extended Properties=Excel 8.0";
    LVL 35

    Accepted Solution

    The connection string with 'Data Source' checks out ok ( ).

    I think you want something like this:
                string sourceConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;  
                           Data Source = {0};
                           Extended Properties=Excel 8.0", files);

    Open in new window


    Author Comment

    If I go with Data Source  = {0}, the result is:

    "Provider=Microsoft.Jet.OLEDB.4.0;  \r\n\r\n                       Data Source = {0};\r\n\r\n                       Extended Properties=Excel 8.0"
    LVL 35

    Expert Comment

    by:Robert Schutt
    Did you forget the
    string.Format("...", files)

    Open in new window

    around that new string?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    17 Experts available now in Live!

    Get 1:1 Help Now