This example will read a csv file into a dataset and save it back when you press button 1 : CSV : Database C# Source Code


Custom Search

C# Source Code » Database » CSV »

 

This example will read a csv file into a dataset and save it back when you press button 1








    



//This example code is from eran.rivlis at gmail.com



   DataTable dt = new DataTable();

       private void Form1_Load(object sender, EventArgs e)
       {
           string conString =  @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\csv" +
                  @";Extended Properties=""Text;HDR=No;FMT=Delimited\""";
           OleDbConnection conn = new OleDbConnection(conString);
           OleDbDataAdapter da = new OleDbDataAdapter(@"Select * from table1.csv", conn);
           da.Fill(dt);
           dataGridView1.DataSource = dt;
       }

       private void button1_Click(object sender, EventArgs e)
       {
           StringBuilder sbCSV = new StringBuilder();
           int intColCount = dt.Columns.Count;
           foreach (DataRowView dr in dt.DefaultView)
           {
                   for (int x = 0; x < intColCount; x++)
                   {
                       sbCSV.Append(dr[x].ToString());
                       if ((x + 1) != intColCount)
                       {
                           sbCSV.Append(",");
                       }
                   }
                   sbCSV.Append("\n");
           }
           using (StreamWriter sw = new StreamWriter(@"c:\csv\table1.csv"))
           {
               sw.Write(sbCSV.ToString());
           }
       } 
           
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» CSV