public static string GetConnectionString(OledDbDriverName driverName, ExtendedProperties extenProps, string filePath) { Dictionary <string, string> props = new Dictionary <string, string>(); // XLSX - Excel 2007, 2010, 2012, 2013 props["Provider"] = GetDriverName(driverName); props["Extended Properties"] = extenProps.GetStringValue(); props["Data Source"] = filePath; // XLS - Excel 2003 and Older //props["Provider"] = "Microsoft.Jet.OLEDB.4.0"; //props["Extended Properties"] = "Excel 8.0"; //props["Data Source"] = "C:\\MyExcel.xls"; StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> prop in props) { sb.Append(prop.Key); sb.Append('='); sb.Append(prop.Value); sb.Append(';'); } return(sb.ToString()); }
private static string GetDriverName(OledDbDriverName oleDbDriverName) { switch (oleDbDriverName) { case OledDbDriverName.MicrosoftACEOLEDB12: return("Microsoft.ACE.OLEDB.12.0"); case OledDbDriverName.MicrosoftJetOLEDB40: return("Microsoft.Jet.OLEDB.4.0"); default: throw new Exception("Driver Not Supported!"); break; } }
public static bool IsOleDbDriverInstalled(OledDbDriverName oleDbDriverName) { string driverName = GetDriverName(oleDbDriverName); var reader = OleDbEnumerator.GetRootEnumerator(); var list = new List <String>(); while (reader.Read()) { for (var i = 0; i < reader.FieldCount; i++) { if (reader.GetName(i) == "SOURCES_NAME") { if (reader.GetValue(i).ToString() == driverName) { reader.Close(); return(true); } } } } reader.Close(); return(false); }