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

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();
Who is Participating?
Robert SchuttConnect With a Mentor Software EngineerCommented:
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

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";
zimmer9Author Commented:
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"
Robert SchuttSoftware EngineerCommented:
Did you forget the
string.Format("...", files)

Open in new window

around that new string?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.