Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c# - Reading A Fixed Format Text File - Part 2

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would use the FileHelpers library to read the fixed length file, rather than ADO.Net.

update In that case I think you need an ini file along side the txt file which defines the txt file. column widths, names, etc.

another option is to forgo ado.net altogether and create a simple fixed length file reader.

var file = new FileInfo("path");
using(var reader = file.Open())
while(reader.Read())
{
   //parse the line
   yield return the object representing the parsed line.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...