Bhaskar Mishra
Bhaskar Mishra

Reputation: 3542

C# issue with oledb connection string for excel

I receive a string like "c:\test\abc.xlsx" which indicates me the excel path. I have to depend on what i receive and cannot hard code it. Now what how should i make sure the the single "\" is escaped and it becomes "\"

string **PATH** = "c:\test\abc.xlsx"
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=**PATH**;Extended Properties=Excel 12.0;";

Upvotes: 1

Views: 3209

Answers (2)

baxter
baxter

Reputation: 42

The single "\" doesn't appear to be escaped properly.

Try: string path = @"c:\test\abc.xlsx";

The leading @ sign will escape it properly for you

Upvotes: 1

DonBoitnott
DonBoitnott

Reputation: 11035

Should be simple:

String thePath = "c:\\test\\abc.xlsx"
String conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + thePath + ";Extended Properties=Excel 12.0;";

Upvotes: 1

Related Questions