This example will read a csv file into a dataset and save it back when you press button 1 : CSV « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » CSVScreenshots 
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());
           }
       
           
           
       
Related examples in the same category
1.Read comma separated value into DataSetRead comma separated value into DataSet
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.