Чтение файла таблицы по расширению при помощи ODBC

Опубликовано Vladimir - вт, 07.10.2008 - 23:00

Добавить комментарий

Содержимое данного поля является приватным и не предназначено для показа.

Filtered HTML

  • Допустимые HTML-теги: <a href hreflang> <em> <strong> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <p> <br><b><i> <img src alt height width>
  • Строки и абзацы переносятся автоматически.
  • Адреса веб-страниц и email-адреса преобразовываются в ссылки автоматически.

Excel 11 (Office 2003)

ConnectionString = @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + filename;

Excel 12 (Office 2007)

ConnectionString = @"DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};ReadOnly=1;PageTimeout=5;MaxScanRows=8;FIL=excel 12.0;DriverId=1046;DBQ=" + filename;

 

public void Connect(string filename)
{
//Check if inputFileName - is dir (usable for multiple dbf files processing)
if (Directory.Exists(filename))
{
basedir = inputFileName;
extension = ""; //take from dir OR ask
}
else
{
basedir = Path.GetDirectoryName(filename);
extension = Path.GetExtension(filename);
}

//Test known formats
switch (extension)
{
case ".dbf":
ConnectionString = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" + basedir;
//ConnectionString = "Provider=VFPOLEDB.1;Data Source =" +filename+ ";";
//ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=dBase III;HDR=Yes";
//ConnectionString = "Provider=MSDASQL/SQLServer ODBC;Driver={Microsoft Visual FoxPro Driver};" + @"SourceType=DBF;SourceDB=" + filename + ";InternetTimeout=300000;Transact Updates=True;Exclusive=No;BackgroundFetch=Yes;Collate=Russian;Null=No;Deleted=No";
//ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=dBASE IV;User ID=Admin;Password=";
/*OdbcConnectionStringBuilder sb = new OdbcConnectionStringBuilder();

sb.Driver = "{Microsoft dBASE Driver (*.dbf)}";
sb.Add("DriverID","277");
sb.Add("Dbq", filename);
string ConnectionString = sb.ConnectionString;
*/
break;
case ".xls":
ConnectionString = @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + filename;
break;
case ".xlsx":
ConnectionString = @"DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};ReadOnly=1;PageTimeout=5;MaxScanRows=8;FIL=excel 12.0;DriverId=1046;DBQ=" + filename;
//                    ConnectionString = @"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm,*.xlsb)};DriverID=1046; READONLY=0;DBQ=" + filename;
//                    ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + "Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";";
break;
case ".csv":
//LumenWorks.Framework.IO.Csv.CsvReader();
break;

default:
throw new Exception("unknown dataset type");
}
inConn = new OdbcConnection(ConnectionString);

}