Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i got problem while reading the following string . this is enclosed in double quotes this is not the problem but this string contain a comma which i want to avoid .i.e. when line string is imported program take it as full string from start of double quote to end of double quote.

here is the string

"X RAY PBH RT IT FEMUR FRACTURE POST OP XRAY -ACCEPTABLE WITH IMPLANT IN SITU 2D ECHO MILD CONC LVH GOOD LV SYSTOLIC FUN, ALTERED LV DIASTOLIC FUN."

i need this data as it is in database so that there will be no problem in exporting.
following is my import method edit it if possible.
C#
public void ImportAllFilesOfFolder()//function declares methods to import//
     {
         try
         {

             using (StreamReader sr = new StreamReader(@"path of file"))
             {
                 //StreamReader sr = new StreamReader(@"C:\Users\Priya\Desktop\B\b.csv");
                 string line = sr.ReadLine();

                // line = line.Replace(",", "");
                 string[] value = line.Split(',');

                 DataTable dt = new DataTable();
                 DataRow row;

                 foreach (string dc in value)
                 {
                     dt.Columns.Add(new DataColumn(dc));
                 }

                 while (!sr.EndOfStream)
                 {
                     value = sr.ReadLine().Split(',');

                     if (value.Length == dt.Columns.Count)
                     {
                         row = dt.NewRow();
                         row.ItemArray = value;
                         dt.Rows.Add(row);
                     }
                 }

                 SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
                 bc.DestinationTableName = "SaiHp";
                 bc.BatchSize = dt.Rows.Count;
                 bc.WriteToServer(dt);
                 bc.Close();
             }
         }

         catch
         {

         }
         finally { con.Close(); }
     }


how to use regex in this code?
Posted
Updated 27-Apr-15 0:39am
v4
Comments
John C Rayan 23-Apr-15 7:08am    
What's your issue with comma? Why do you want to avoid? Do you want to remove comma from the string?

1 solution

A line.Split(',') is just to simple to parse a csv file correctly. I would really advize using something like filehelpers. It is opensource, free and very easy to use. It also has lots of examples and works much better.

http://filehelpers.sourceforge.net/[^]

You could also check out this:
A Fast CSV Reader[^]

Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900