读取固定格式的文本文件 - 第2部分 [英] Reading A Fixed Format Text File - Part 2

查看:665
本文介绍了读取固定格式的文本文件 - 第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对问题的后续问题:



我正在尝试使用Microsoft.ACE.OLEDB.12.0提供程序读取固定格式的文本文件。我有六个不同的方式来设置驱动程序和/或提供程序,几乎遇到相同的问题,每次。我由于某些原因无法启动,因为无法找到可安装的ISAM异常或错误和驱动程序的异常。



系统有Office 2007



有没有人看到下面的代码的问题?

  string DATABASE_PROVIDER =Provider = Microsoft.ACE.OLEDB.12.0; 
string CVS = Application.StartupPath + @\Data.txt;
string connectionString = DATABASE_PROVIDER =;数据源=+ CVS +;扩展属性=文本; HDR =是; FMT =固定;
string field =*;
string table =Data;
string StringQueryCMD =SELECT+字段+FROM+表;
OleDbConnection myConnection = new OleDbConnection(connectionString);
OleDbCommand cmd = myConnection.CreateCommand();
cmd.CommandText = StringQueryCmd;
myConnection.Open(); //< ----无法找到可安装的ISAM例外
OleDataAdapter myDataAdapter = new OleDbDataAdapter(cmd);
DataTable Table = new DataTable(Data); //< ----无法找到可安装的ISAM例外

myDataAdapter.Fill(Table);


解决方案

无法找到可安装的ISAM异常的解决方案是使用以下命令:

  string EXTENDED_PROPERTIES = @ Text; HDR = YES; FMT = FixedLength;; 

解决方案的关键在于扩展属性值,我能够填充DataTable的文件的内容,我认为有一个问题的ini文件,所以它包含字符串-----这对我没有用。

 字符串DATABASE_PROVIDER = p> 

Provider = Microsoft.ACE.OLEDB.12.0;
string CVS Application.StartupPath +\\Database.accdb;
string DATA_SOURCE =数据源+ CVS;
string connectionString = DATABASE_PROVIDER + DATA_SOURCE;
string TABLE =FROM STUFF;
string SELECT =SELECT CODE,NAME,ICON,FUNCTION;
string StringQueryCmd = SELECT + TABLE;

OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbCommand Command = OleDbCommand(StringQueryCmd,MyConnection);
OleDbAdapter MyDataAdapter = new OleDbAdapter(Command);
DataSet MyDataSet = new DataSet();
DataTable MyDataTable = new DataTable();
MyConnection.Open();
MyDataAdapter.Fill(MyDataSet,STUFF);
MyConnection.Close();

一旦你有一个DataTable,你可以在理论上使用LINQ to DataSet,而不是处理DataTable。


This is a followup question to the question:

I am attempting to read a Fixed Format Text file using the Microsoft.ACE.OLEDB.12.0 Provider. I have a half dozen different ways to setup the driver and/or provider and pretty much run into the same problem every time. I am for some reason unable to even "get started" because of "Could not find installable ISAM" exceptions or errors and exceptions with the driver.

The system has Office 2007 installed so the "Could not find installable ISAM" does not make a great deal of sense.

Does anyone see the problem with the following code?

string DATABASE_PROVIDER = "Provider=Microsoft.ACE.OLEDB.12.0";
string CVS = Application.StartupPath + @"\Data.txt";
string connectionString = DATABASE_PROVIDER = ";Data Source=" + CVS +";Extended Properties=text;HDR=Yes;FMT=Fixed";
string field ="*";
string table ="Data";
string StringQueryCMD = "SELECT" + field+" FROM " + table;
OleDbConnection myConnection = new OleDbConnection( connectionString );
OleDbCommand cmd = myConnection.CreateCommand();
cmd.CommandText = StringQueryCmd;
myConnection.Open(); // <---- "Could not find installable ISAM" exception here
OleDataAdapter myDataAdapter = new OleDbDataAdapter(cmd);
DataTable Table = new DataTable("Data");// <---- "Could not find installable ISAM" exception here

myDataAdapter.Fill(Table);

解决方案

I ended up going with a slightly different solution. The solution to the "Could not find installable ISAM" exception was to use the following:

string EXTENDED_PROPERTIES = @"Extended Properties=""Text;HDR=YES;FMT=FixedLength;""";

The key to the solution is the "(s) around the "Extended Properties" values. I was able to populate the DataTable with the contents of the file, I think there was a problem with the ini file, so it contained strings of "-----" which was useless to me.

So I ended up simply reading the access database.

string DATABASE_PROVIDER = "Provider=Microsoft.ACE.OLEDB.12.0";
string CVS Application.StartupPath + ""\\Database.accdb";
string DATA_SOURCE = "Data Source" + CVS;
string connectionString = DATABASE_PROVIDER + DATA_SOURCE;
string TABLE = " FROM STUFF";
string SELECT = "SELECT CODE, NAME, ICON, FUNCTION;
string StringQueryCmd = SELECT + TABLE;

OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbCommand Command = OleDbCommand(StringQueryCmd,MyConnection);
OleDbAdapter MyDataAdapter = new OleDbAdapter(Command);
DataSet MyDataSet = new DataSet();
DataTable MyDataTable = new DataTable();
MyConnection.Open();
MyDataAdapter.Fill(MyDataSet,"STUFF");
MyConnection.Close();

Once you have a DataTable you could in theory use LINQ to DataSet instead of dealing with the DataTable.

这篇关于读取固定格式的文本文件 - 第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆