473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Importing CSV File - Text is losing its value

Hello All:

I am importing an Excel / CSV file. The problem I am having is: the
columns are being defined for me as int32 (able to determine by using the
..GetFieldType method). As a result: when a row has a character, we are
losing this data, because it is being interprested as "". How would I go
about making the columns always text? Below is my code that I am using:

System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=" + System.IO.Path.GetDirectoryName(fileName) + ";Extended
Properties=\"text;HDR=Yes;IMEX=1;FMT=Delimited\"") ;

System.Data.OleDb.OleDbCommand command = new
System.Data.OleDb.OleDbCommand("Select * from " +
System.IO.Path.GetFileName(fileName), con);
con.Open();
System.Data.IDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if(dr[0].ToString()!="")
if(!CSVvalues.ContainsKey(dr[0].ToString()))
CSVvalues.Add(dr[0].ToString(), dr[1].ToString());
}

Aug 23 '06 #1
5 5837
Hi,

If you know for sure it will be csv use the provider from opennetcf.org
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Andy" <An**@discussions.microsoft.comwrote in message
news:A2**********************************@microsof t.com...
Hello All:

I am importing an Excel / CSV file. The problem I am having is: the
columns are being defined for me as int32 (able to determine by using the
.GetFieldType method). As a result: when a row has a character, we are
losing this data, because it is being interprested as "". How would I go
about making the columns always text? Below is my code that I am using:

System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=" + System.IO.Path.GetDirectoryName(fileName) + ";Extended
Properties=\"text;HDR=Yes;IMEX=1;FMT=Delimited\"") ;

System.Data.OleDb.OleDbCommand command = new
System.Data.OleDb.OleDbCommand("Select * from " +
System.IO.Path.GetFileName(fileName), con);
con.Open();
System.Data.IDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if(dr[0].ToString()!="")
if(!CSVvalues.ContainsKey(dr[0].ToString()))
CSVvalues.Add(dr[0].ToString(), dr[1].ToString());
}

Aug 23 '06 #2
No solution, I'm afraid, but you could save yourself a lot of grief and have
a look at:
http://www.codeproject.com/cs/database/CsvReader.asp

You get the CSV records back as string arrays, so you can do whatever you
need to in order to process them. It's also VERY fast.

Steve
"Andy" <An**@discussions.microsoft.comwrote in message
news:A2**********************************@microsof t.com...
Hello All:

I am importing an Excel / CSV file. The problem I am having is: the
columns are being defined for me as int32 (able to determine by using the
.GetFieldType method). As a result: when a row has a character, we are
losing this data, because it is being interprested as "". How would I go
about making the columns always text? Below is my code that I am using:

System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=" + System.IO.Path.GetDirectoryName(fileName) + ";Extended
Properties=\"text;HDR=Yes;IMEX=1;FMT=Delimited\"") ;

System.Data.OleDb.OleDbCommand command = new
System.Data.OleDb.OleDbCommand("Select * from " +
System.IO.Path.GetFileName(fileName), con);
con.Open();
System.Data.IDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if(dr[0].ToString()!="")
if(!CSVvalues.ContainsKey(dr[0].ToString()))
CSVvalues.Add(dr[0].ToString(), dr[1].ToString());
}

Aug 23 '06 #3
We would prefer to not have to use a third-party tool when the functionality
has to be there in .Net. We just need to know how to do it as opposed to
having to maintain a third party component for something as simple as reading
a .csv file.

Thanks though for your help.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

If you know for sure it will be csv use the provider from opennetcf.org
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Andy" <An**@discussions.microsoft.comwrote in message
news:A2**********************************@microsof t.com...
Hello All:

I am importing an Excel / CSV file. The problem I am having is: the
columns are being defined for me as int32 (able to determine by using the
.GetFieldType method). As a result: when a row has a character, we are
losing this data, because it is being interprested as "". How would I go
about making the columns always text? Below is my code that I am using:

System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=" + System.IO.Path.GetDirectoryName(fileName) + ";Extended
Properties=\"text;HDR=Yes;IMEX=1;FMT=Delimited\"") ;

System.Data.OleDb.OleDbCommand command = new
System.Data.OleDb.OleDbCommand("Select * from " +
System.IO.Path.GetFileName(fileName), con);
con.Open();
System.Data.IDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if(dr[0].ToString()!="")
if(!CSVvalues.ContainsKey(dr[0].ToString()))
CSVvalues.Add(dr[0].ToString(), dr[1].ToString());
}


Aug 23 '06 #4
Hi,

You can have the source code and just customize it to your need.

It will be a couple of methods at the most to just read the csv and have a
string[]

IIRC the problem with using the OleDB is that it looks at the top X rows to
decide the type of the columns

The solution I offfered you just return a string[] that you can convert to
your need.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andy" <An**@discussions.microsoft.comwrote in message
news:FA**********************************@microsof t.com...
We would prefer to not have to use a third-party tool when the
functionality
has to be there in .Net. We just need to know how to do it as opposed to
having to maintain a third party component for something as simple as
reading
a .csv file.

Thanks though for your help.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
>Hi,

If you know for sure it will be csv use the provider from opennetcf.org
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Andy" <An**@discussions.microsoft.comwrote in message
news:A2**********************************@microso ft.com...
Hello All:

I am importing an Excel / CSV file. The problem I am having is: the
columns are being defined for me as int32 (able to determine by using
the
.GetFieldType method). As a result: when a row has a character, we
are
losing this data, because it is being interprested as "". How would I
go
about making the columns always text? Below is my code that I am
using:

System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=" + System.IO.Path.GetDirectoryName(fileName) + ";Extended
Properties=\"text;HDR=Yes;IMEX=1;FMT=Delimited\"") ;

System.Data.OleDb.OleDbCommand command = new
System.Data.OleDb.OleDbCommand("Select * from " +
System.IO.Path.GetFileName(fileName), con);
con.Open();
System.Data.IDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if(dr[0].ToString()!="")
if(!CSVvalues.ContainsKey(dr[0].ToString()))
CSVvalues.Add(dr[0].ToString(),
dr[1].ToString());
}



Aug 23 '06 #5

..GetValue retrieves an object.
You could then cast as a string
object o = dataReader.GetValue();

string s = Convert.ToString(o); // or .ToString() method.

...

Or does that not work , because something is happening on the front side of
it??

"Andy" <An**@discussions.microsoft.comwrote in message
news:A2**********************************@microsof t.com...
Hello All:

I am importing an Excel / CSV file. The problem I am having is: the
columns are being defined for me as int32 (able to determine by using the
.GetFieldType method). As a result: when a row has a character, we are
losing this data, because it is being interprested as "". How would I go
about making the columns always text? Below is my code that I am using:

System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=" + System.IO.Path.GetDirectoryName(fileName) + ";Extended
Properties=\"text;HDR=Yes;IMEX=1;FMT=Delimited\"") ;

System.Data.OleDb.OleDbCommand command = new
System.Data.OleDb.OleDbCommand("Select * from " +
System.IO.Path.GetFileName(fileName), con);
con.Open();
System.Data.IDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if(dr[0].ToString()!="")
if(!CSVvalues.ContainsKey(dr[0].ToString()))
CSVvalues.Add(dr[0].ToString(), dr[1].ToString());
}

Aug 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: expect | last post by:
What's the big deal with importing text files? I have a 70 MB file to import and it's been one problem after another. I used the copy command and it appears that it's just not possible. I finally...
11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
2
by: nutthatch | last post by:
I want to be able to import an Excel spreadsheet into Access 2K using the macro command Transferspreadsheet. However, the file I am importing (over which I have no control) contains some records...
0
by: Kucho | last post by:
Hi there, I'm trying to import a text file direct into a MS Access database with the folowing command line : INSERT INTO < table name > SELECT * FROM ." Even though my MS Access table is a...
0
by: Andy | last post by:
Hello All: I am importing an Excel / CSV file. The problem I am having is: the columns are being defined for me as int32 (able to determine by using the ..GetFieldType method). As a result: ...
2
by: RRoma | last post by:
Does anybody know how I can import lists of numbers from Excel into Access without losing the preceeding Zeros??? The field must have a total of 10 digits.... Is this a function of how its saved in...
3
by: gulllet | last post by:
I try to import a tab separated text file into sql server 2005 using the import guide. But when running the job I get the error message Error 0xc02020c5: Data Flow Task: Data conversion failed...
2
by: Debbiedo | last post by:
I have a text file that I am importing into an Access table that was generatred from data exported from a Word file. Several (about 20-30) fields are from check boxes on the Word form. These fields...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.