Csharp/C Sharp/Database ADO.net/CSV

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Read comma separated value into DataSet

using System;
using System.Data;
using System.IO;
class Class1{
    static void Main(string[] args){
         DataSet myDataSet = GetData();
         foreach (DataColumn c in myDataSet.Tables["TheData"].Columns){
            Console.Write("{0,-20}",c.ColumnName);
         }
         Console.WriteLine();
         foreach (DataRow r in myDataSet.Tables["TheData"].Rows)
         {
            foreach (DataColumn c in myDataSet.Tables["TheData"].Columns)
            {
               Console.Write("{0,-20}",r[c]);
            }
            Console.WriteLine();
         }
    }
    private static DataSet GetData(){
         string strLine;
         string[] strArray;
         char[] charArray = new char[] {","};
         DataSet ds = new DataSet();
         DataTable dt = ds.Tables.Add("TheData");
         FileStream aFile = new FileStream("csv.txt",FileMode.Open);
         StreamReader sr = new StreamReader(aFile);
         strLine = sr.ReadLine();
         strArray = strLine.Split(charArray);
         
         for(int x=0;x<=strArray.GetUpperBound(0);x++) {
            dt.Columns.Add(strArray[x].Trim());
         }
         
         strLine = sr.ReadLine();
         while(strLine != null) {
            strArray = strLine.Split(charArray);
            DataRow dr = dt.NewRow();
            for(int i=0;i<=strArray.GetUpperBound(0);i++) {
               dr[i] = strArray[i].Trim();
            }
            dt.Rows.Add(dr);
            strLine = sr.ReadLine();
         }
         sr.Close();
         return ds;
    }
}
// File: csv.txt
/*
1,2,3,4
5,6,7,8
*/


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.ru

   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());
           }
       }