PC Review


Reply
Thread Tools Rate Thread

csv header captions trimmed [urgent][?!]

 
 
Wiktor Zychla
Guest
Posts: n/a
 
      21st Jan 2005
Just a while ago I've tried to solve an issue with reading CSV files and
stepped into an issue where the header text is stripped if longer than 64
chars!

this is unexpected. what's interesting is that Excel read this CSV file
correctly.

can I really write my own CSV reading routine to solve that issue or can
anything easier be done to fix that? this constraint is new to me, I've
never found any documents saying that header data cannot exceed the legth of
64 chars.

thanks in advance
Wiktor Zychla

details: two drivers tested (JET.4.0 via OleDbc and Microsoft Text Driver
via Odbc). both trim the header caption to the length of 64 chars.

code:

using System;
using System.Data;
using System.Data.OleDb;
using System.Data.Odbc;
using System.IO;

class T {
public static void Main()
{
using ( IDbConnection conn = new OdbcConnection() )
{
try
{
string fileName = "c:\\003\\test.csv";

//conn.ConnectionString =
String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended
Properties=\"text;HDR=Yes;FMT=Delimited\"",
Path.GetDirectoryName(fileName) );
conn.ConnectionString = String.Format( "Driver={{Microsoft Text Driver
(*.txt; *.csv)}};DBQ={0};Extensions=asc,csv,tab,txt;HDR=YES;Persist Security
Info=False", Path.GetDirectoryName(fileName) );
conn.Open();

// inicjuj dataset
IDataAdapter da = new OdbcDataAdapter( string.Format( "SELECT * FROM
[{0}]", Path.GetFileName(fileName) ), (OdbcConnection)conn );
DataSet ds = new DataSet();
da.Fill( ds );
DataTable dt = ds.Tables[0];

Console.WriteLine( dt.Columns[0].Caption );
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}
}
}
}

data [test.csv]:

Z123456789Z123456789Z123456789Z123456789Z123456789Z123456789Z123456789Z123456789
1


 
Reply With Quote
 
 
 
 
Paul Clement
Guest
Posts: n/a
 
      21st Jan 2005
On Fri, 21 Jan 2005 11:45:31 +0100, "Wiktor Zychla" <(E-Mail Removed)> wrote:

¤ Just a while ago I've tried to solve an issue with reading CSV files and
¤ stepped into an issue where the header text is stripped if longer than 64
¤ chars!
¤
¤ this is unexpected. what's interesting is that Excel read this CSV file
¤ correctly.
¤
¤ can I really write my own CSV reading routine to solve that issue or can
¤ anything easier be done to fix that? this constraint is new to me, I've
¤ never found any documents saying that header data cannot exceed the legth of
¤ 64 chars.
¤
¤ thanks in advance
¤ Wiktor Zychla
¤
¤ details: two drivers tested (JET.4.0 via OleDbc and Microsoft Text Driver
¤ via Odbc). both trim the header caption to the length of 64 chars.
¤
¤ code:
¤
¤ using System;
¤ using System.Data;
¤ using System.Data.OleDb;
¤ using System.Data.Odbc;
¤ using System.IO;
¤
¤ class T {
¤ public static void Main()
¤ {
¤ using ( IDbConnection conn = new OdbcConnection() )
¤ {
¤ try
¤ {
¤ string fileName = "c:\\003\\test.csv";
¤
¤ //conn.ConnectionString =
¤ String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended
¤ Properties=\"text;HDR=Yes;FMT=Delimited\"",
¤ Path.GetDirectoryName(fileName) );
¤ conn.ConnectionString = String.Format( "Driver={{Microsoft Text Driver
¤ (*.txt; *.csv)}};DBQ={0};Extensions=asc,csv,tab,txt;HDR=YES;Persist Security
¤ Info=False", Path.GetDirectoryName(fileName) );
¤ conn.Open();
¤
¤ // inicjuj dataset
¤ IDataAdapter da = new OdbcDataAdapter( string.Format( "SELECT * FROM
¤ [{0}]", Path.GetFileName(fileName) ), (OdbcConnection)conn );
¤ DataSet ds = new DataSet();
¤ da.Fill( ds );
¤ DataTable dt = ds.Tables[0];
¤
¤ Console.WriteLine( dt.Columns[0].Caption );
¤ }
¤ catch( Exception ex )
¤ {
¤ Console.WriteLine( ex.Message );
¤ }
¤ }
¤ }
¤ }
¤
¤ data [test.csv]:
¤
¤ Z123456789Z123456789Z123456789Z123456789Z123456789Z123456789Z123456789Z123456789
¤ 1
¤

I haven't seen this before, but then again I've never had a column name of that length - can't
imagine why it would be necessary. I think Excel just imports it all as data so there is no need to
determine column names.

You can rename (alias) your columns by either using a schema.ini file or via the SQL statement. You
should set HDR=NO in your connection string and use the default column names.

SELECT F1 As NewColName, F2 As AnotherAliasedColumnName FROM [LongColNames#txt]"

Schema.ini doc:
http://msdn.microsoft.com/library/de...a_ini_file.asp


Paul ~~~ (E-Mail Removed)
Microsoft MVP (Visual Basic)
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table captions converted to figure captions, & vice versa =?Utf-8?B?YnVmb3NzaWw=?= Microsoft Word Document Management 12 18th May 2007 08:10 AM
Captions, multiple captions on the same line =?Utf-8?B?YWxsYW4=?= Microsoft Word Document Management 1 8th Mar 2005 08:33 PM
URGENT - Need help with Header with Footer =?Utf-8?B?WGVybw==?= Microsoft Word Document Management 6 26th Jan 2005 10:02 PM
Image Hot Spots with Captions: Mouse Roll Over to Display Captions? J. Danniel Microsoft Frontpage 6 27th Feb 2004 01:45 AM
Datagrid Header Urgent!!!!!! Chris Calhoun Microsoft C# .NET 4 28th Sep 2003 02:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:12 AM.