You must Sign In to post a response.
  • Category: Visual Studio

    How to read string of csv file having comma in double quote?

    i have csv file in which string quoted in double quote and contains comma and i set comma as delimiter so while importing it not take that record so help me to sort out this issue.

    here is file

    PTNAME,REGNO/ID,BLOOD GRP,WARD NAME,DOC NAME,XRAY,PATHO,MEDICATION,BLOOD GIVEN
    Mr. GHULAVE VASANTRAO PANDURANG,SH1503/00847,,RECOVERY,SHELKE SAMEER,"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.", HB-11.9gm% TLC-8700 PLT COUNT-195000 BSL-173 UREA -23 CREATININE -1.2 SR.ELECTROLYTES-WNR BLD GROUP-B + HIV-NEGATIVE HBsAG-NEGATIVE PT INR -15/15/1.0. ECG SINUS TACHYCARDIA ,IV TAXIMAX 1.5 GM 1-0-1 IV TRAMADOL DRIP 1-0-1 TAB NUSAID SP 1-0-1 TAB ARCOPAN D 1-0-1 CAP BONE C PLUS 1 -0-1 TAB ANXIT 0.5 MG 0-0-1 ANKLE TRACTION 3 KG RT LL ,NOT GIVEN

    and here is my import and save code

    public void ImportAllFilesOfFolder()//function declares methods to import//
    {
    try
    {
    con.Open();
    string sourceDir = txtsend.Text;
    var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv");

    foreach (string currentFile in IcsvFile)
    {
    using (StreamReader sr = new StreamReader(currentFile))
    {
    string line = sr.ReadLine();
    string[] value = line.Trim('"').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 = "SaiRamH";
    bc.BatchSize = dt.Rows.Count;
    bc.WriteToServer(dt);
    bc.Close();
    }
    }
    }

    catch
    {
    //MessageBox.Show(e.Message);
    }
    finally { con.Close(); }
    }

    public void set()
    {
    try
    {
    SqlDataAdapter da = new SqlDataAdapter("SELECT * from SaiRamH", con);
    //DataSet ds = new DataSet();
    da.Fill(saiRamDataSet11, "SaiRamH");
    dataGridView1.DataSource = saiRamDataSet11.Tables["SaiRamH"];
    con.Close();
    }
    catch { }
    }
  • #758354
    if possible give me example of regex and with file helpers according to my code.


  • Sign In to post your comments