PC Review


Reply
Thread Tools Rate Thread

CSV to DataSet does not respect empty strings

 
 
=?Utf-8?B?a2V2aW4=?=
Guest
Posts: n/a
 
      22nd Jun 2006
using vs 2003
the csv file has every field value enclosed in double quotes: i.e. no null
values

Whe using OleDbDataAdapter to read a csv file (I do create the schema.ini
file defining all columns as text) into a DataTable, fields that are an empty
string ("") are set to DBNull in my data table. I need to serialize this
dataset to an xml file and the lack of an empty node for all fields is a
problem.

Any solutions?

a short program follows

*****test.csv*****
FirstName,LastName,Sex
"kevin","williams","m"
"bob","le flambeur",""
*****test.csv*****

****schema.ini****
[test.csv]
ColNameHeader=True
Format=CSVDelimited
MaxScanRows=0
CharacterSet=OEM
Col1=FIRSTNAME TEXT
Col2=LASTNAME TEXT
Col3=SEX TEXT
****schema.ini****

****class1.cs****
class class1
{
[MTAThread]
static void Main(string[] args)
{

string constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\mmproc\\;Extended
Properties=\"text;HDR=Yes;FMT=Delimited;IMAX=1\"";

DataSet ds = new DataSet("MyDataSet");
ds.Tables.Add(new DataTable("MyTable"));
DataTable dt = ds.Tables[0];

dt.Columns.Add("FirstName",typeof(string));
dt.Columns.Add("LastName", typeof(string));
DataColumn sexCol = dt.Columns.Add("Sex",typeof(string));
sexCol.DefaultValue = "";

OleDbCommand cmd = new OleDbCommand("select * from [test.csv]");
cmd.Connection = new OleDbConnection(constring);
cmd.CommandType = CommandType.Text;
OleDbDataAdapter adp= new OleDbDataAdapter();

using(OleDbConnection con = cmd.Connection)
{
con.Open();
adp.SelectCommand = cmd;
adp.Fill(dt);
}
ds.WriteXml("c:\\mmproc\\kevinDataset.xml");
}// main
}
****class1.cs****

****kevinDataset.xml****
<?xml version="1.0" standalone="yes"?>
<MyDataSet>
<MyTable>
<FirstName>kevin</FirstName>
<LastName>williams</LastName>
<Sex>m</Sex>
</MyTable>
<MyTable>
<FirstName>bob</FirstName>
<LastName>le flambeur</LastName>
</MyTable>
</MyDataSet>
****kevinDataset.xml****
--
kevin...
 
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
How can I convert empty strings to empty cells? =?Utf-8?B?U2hhbmU=?= Microsoft Excel Misc 2 19th Jul 2005 12:10 PM
Interpret empty datagrid cells as empty strings Thomas Berg Microsoft Dot NET Framework Forms 2 2nd Jun 2004 10:54 AM
Re: Empty Strings CJ Taylor Microsoft VB .NET 9 30th Sep 2003 08:20 AM
Re: Empty Strings Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 0 27th Sep 2003 05:54 PM
Empty strings Chris Capel Microsoft C# .NET 4 7th Aug 2003 11:48 PM


Features
 

Advertising
 

Newsgroups
 


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