0
    string _filePath = @"C:\Users\szhao\Downloads\USER_UploadCSV.xlsx";
    FileInfo fi = new FileInfo(_filePath);
    string _connectionString = "Provider=Microsoft.JET.OLEDB.4.0;Mode=Read;Data Source=" + _filePath + "; Extended Properties=\"Excel 8.0;HDR=Yes;\"";
    if (fi.Extension.Equals(".xlsx") || fi.Extension.Equals(".xls"))
    {
        // For Excel 2007 File  Format
        _connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Data Source=" + _filePath + " ;Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
    }
    OleDbConnection _conn = new OleDbConnection(_connectionString);
    _conn.Open();
    Console.WriteLine("Connect!");

Mode=Readin connectionstring は、これが読み取り専用モードであることを示している必要があります。これにより、複数のアプリケーションが書き込みなしで単一の Excel を読み取ることができます。しかし、それはで例外をスローする_conn.Open();ので、接続文字列が間違っているのではないかと思いましたか?

以下は例外メッセージです

Exception Message:
External table is not in the expected format.

Stack Trace:
   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OleDb.OleDbConnection.Open()
   at TIDEAPI.BLL.AExcelFile..ctor(TestAdministration pTestAdministration, String pFilePath, UploadFileSpecification pUploadFileSpecification) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\Abstraction\AExcelFile.cs:line 41
   at TIDEAPI.BLL.ExcelFile..ctor(TestAdministration pTestAdministration, String pFilePath, UploadFileSpecification pUploadFileSpecification) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\ExcelFile.cs:line 14
   at TIDEAPI.BLL.AcceptableFileFactory.GetAcceptableFile(TestAdministration pTestAdministration, String pFilePath, UploadFileSpecification pUploadFileSpecification) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\AcceptableFileFactory.cs:line 22
   at TIDEAPI.BLL.AFileProcessor..ctor(Int64 pTestAdministartionKey, String pFilePath, UploadFileSpecification pUploadFileSpecification, FrequencyDistributionLogger fdLogger, DataExchangeErrorLogger errorLogger, IDictionary`2 dataExchangeProperties) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\Abstraction\AFileProcessor.cs:line 108
   at TIDEAPI.BLL.UserFileProcessor..ctor(Int64 pTestAdministrationKey, String pTideFile, UploadFileSpecification pUploadFileSpecification, FrequencyDistributionLogger fdLogger, DataExchangeErrorLogger errorLogger, IDictionary`2 dataExchangeProperties) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\UserFileProcessor.cs:line 32
   at TIDEAPI.BLL.FileProcessorFactory.GetFileProcessor(Int64 pTestAdministrationKey, String pFilePath, UploadFileSpecification pUploadFileSpecification, FrequencyDistributionLogger fdLogger, DataExchangeErrorLogger errorLogger, FileProcessorTypes pFileProcessorType, IDictionary`2 dataExchangeProperties) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\FileProcessorFactory.cs:line 42
   at PreIdService.PreIdFile.Load(Object pWorkerThreadParameters) in c:\Projects\PreProcessing\TIDE\Online\Tools\TideBatchUploadService\PreIdFile.cs:line 62

Mode=Read を削除すると、複数のアプリケーションが読み取れなくなりますが、シングル ユーザーは機能します。

4

1 に答える 1