PC Review


Reply
Thread Tools Rate Thread

dealing with commas in fields of csv file

 
 
Mike P
Guest
Posts: n/a
 
      6th Sep 2006
I am trying to write a csv file to a database table, but the problem is
that several fields have commas within them, so the code is reading the
commas as the end of fields. My code is below. How do I get around
this?

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

StreamReader sr = new
StreamReader("\\\\devext02\\Xerox_Upload\\archive\\" + strFileName +
".csv");
string fullFileStr = sr.ReadToEnd();
sr.Close();
sr.Dispose();

string[] lines = fullFileStr.Split('\n');
DataTable dt = new DataTable();

string[] sArr = lines[0].Split(',');

foreach (string s in sArr)
{
dt.Columns.Add(new DataColumn());
}

DataRow row;
string finalLine = "";

foreach (string line in lines)
{
row = dt.NewRow();
finalLine = line.Replace(Convert.ToString('\r'), "");
row.ItemArray = finalLine.Split(',');
dt.Rows.Add(row);
}

SqlConnection objConnection = new
SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"]
ConnectionString);

System.Data.SqlClient.SqlBulkCopy bc = new
System.Data.SqlClient.SqlBulkCopy(objConnection,
SqlBulkCopyOptions.TableLock, null);

bc.BatchSize = dt.Rows.Count;
objConnection.Open();
bc.DestinationTableName = "UploadDataStaging";
bc.WriteToServer(dt);
objConnection.Close();
bc.Close();
TimeSpan ts = stopWatch.Elapsed;
stopWatch.Stop();



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      6th Sep 2006
Mike,

If it is a CSV file, instead of trying to parse it yourself, why not use
the text provider for OLEDB and use the classes in the System.Data.OleDb
namespace to access the contents of the file as a data set?


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Mike P" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am trying to write a csv file to a database table, but the problem is
> that several fields have commas within them, so the code is reading the
> commas as the end of fields. My code is below. How do I get around
> this?
>
> Stopwatch stopWatch = new Stopwatch();
> stopWatch.Start();
>
> StreamReader sr = new
> StreamReader("\\\\devext02\\Xerox_Upload\\archive\\" + strFileName +
> ".csv");
> string fullFileStr = sr.ReadToEnd();
> sr.Close();
> sr.Dispose();
>
> string[] lines = fullFileStr.Split('\n');
> DataTable dt = new DataTable();
>
> string[] sArr = lines[0].Split(',');
>
> foreach (string s in sArr)
> {
> dt.Columns.Add(new DataColumn());
> }
>
> DataRow row;
> string finalLine = "";
>
> foreach (string line in lines)
> {
> row = dt.NewRow();
> finalLine = line.Replace(Convert.ToString('\r'), "");
> row.ItemArray = finalLine.Split(',');
> dt.Rows.Add(row);
> }
>
> SqlConnection objConnection = new
> SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"]
> ConnectionString);
>
> System.Data.SqlClient.SqlBulkCopy bc = new
> System.Data.SqlClient.SqlBulkCopy(objConnection,
> SqlBulkCopyOptions.TableLock, null);
>
> bc.BatchSize = dt.Rows.Count;
> objConnection.Open();
> bc.DestinationTableName = "UploadDataStaging";
> bc.WriteToServer(dt);
> objConnection.Close();
> bc.Close();
> TimeSpan ts = stopWatch.Elapsed;
> stopWatch.Stop();
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      6th Sep 2006
Hi,

A " character is used to enclose a field that contains a comma as part of
hte values.

The easiest solution can be or either use OleDb data provider or go to
opennetcf.org and download theirs, you will have the source code that you
can browse/modify at will



--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




"Mike P" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am trying to write a csv file to a database table, but the problem is
> that several fields have commas within them, so the code is reading the
> commas as the end of fields. My code is below. How do I get around
> this?
>
> Stopwatch stopWatch = new Stopwatch();
> stopWatch.Start();
>
> StreamReader sr = new
> StreamReader("\\\\devext02\\Xerox_Upload\\archive\\" + strFileName +
> ".csv");
> string fullFileStr = sr.ReadToEnd();
> sr.Close();
> sr.Dispose();
>
> string[] lines = fullFileStr.Split('\n');
> DataTable dt = new DataTable();
>
> string[] sArr = lines[0].Split(',');
>
> foreach (string s in sArr)
> {
> dt.Columns.Add(new DataColumn());
> }
>
> DataRow row;
> string finalLine = "";
>
> foreach (string line in lines)
> {
> row = dt.NewRow();
> finalLine = line.Replace(Convert.ToString('\r'), "");
> row.ItemArray = finalLine.Split(',');
> dt.Rows.Add(row);
> }
>
> SqlConnection objConnection = new
> SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"]
> ConnectionString);
>
> System.Data.SqlClient.SqlBulkCopy bc = new
> System.Data.SqlClient.SqlBulkCopy(objConnection,
> SqlBulkCopyOptions.TableLock, null);
>
> bc.BatchSize = dt.Rows.Count;
> objConnection.Open();
> bc.DestinationTableName = "UploadDataStaging";
> bc.WriteToServer(dt);
> objConnection.Close();
> bc.Close();
> TimeSpan ts = stopWatch.Elapsed;
> stopWatch.Stop();
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
Mike P
Guest
Posts: n/a
 
      7th Sep 2006
Nicholas,

Do you have an example of using this method?


Thanks,

Mike



*** Sent via Developersdex http://www.developersdex.com ***
 
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
Need Help dealing with blank fields JH Microsoft Access VBA Modules 3 14th Dec 2009 08:10 PM
dealing with max number of fields =?Utf-8?B?UGhyZWRk?= Microsoft Access External Data 5 11th Aug 2006 09:51 AM
Splitting fields from commas loopkid1 Microsoft Excel Programming 1 22nd Jun 2005 08:12 PM
Re: Splitting fields from commas Ron Coderre Microsoft Excel Programming 0 22nd Jun 2005 07:41 PM
commas in form fields Leesa Microsoft Access ADP SQL Server 1 28th Aug 2003 01:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 PM.