简体   繁体   中英

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;";

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;";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM