无法使用oledb打开csv文件的连接

时间:2011-12-28 07:20:33

标签: c# asp.net .net csv

我正在使用此函数从csv文件中获取数据集:

public static DataSet GetDataset(string filePathName, string fileName, string fieldNames)
    {
        OleDbConnection CSVConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePathName + ";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';");
        OleDbCommand CSVCommand = new OleDbCommand(@"SELECT " + fieldNames + " FROM [" + fileName + "]", CSVConnection);
        OleDbDataAdapter CSVAdapter = new OleDbDataAdapter(CSVCommand);
        DataSet CSVDataSet = new DataSet();
        try
        {
            CSVConnection.Open();
            CSVAdapter.Fill(CSVDataSet);
            return CSVDataSet;
        }
        catch(Exception ex)
        {
            return CSVDataSet;
        }
        finally
        {
            CSVConnection.Close();
            CSVConnection.Dispose();
        }
    }

但是,在使用oledb提供程序

读取csv文件时,我在服务器上遇到以下错误
  

消息:未指定错误       StackTrace:在System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString)   constr,OleDbConnection连接)at   System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions   options,Object poolGroupProviderInfo,DbConnectionPool池,   DbConnection owningObject)at   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(的DbConnection   owningConnection,DbConnectionPoolGroup poolGroup)at   System.Data.ProviderBase.DbConnectionFactory.GetConnection(的DbConnection   拥有连接)   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(的DbConnection   outerConnection,DbConnectionFactory connectionFactory)at   DB.GetDataset的System.Data.OleDb.OleDbConnection.Open()(String   filePathName,String fileName,String fieldNames)

1 个答案:

答案 0 :(得分:0)

查看连接字符串的模板将有助于找出问题所在。 试着看看Connection string formats here