[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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
Medium Priority
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
  • 2
LVL 53

Expert Comment

ID: 38286679
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

Robert Schutt earned 2000 total points
ID: 38286703
The connection string with 'Data Source' checks out ok ( http://www.connectionstrings.com/excel ).

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

ID: 38287966
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
ID: 38287976
Did you forget the
string.Format("...", files)

Open in new window

around that new string?

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 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