指定路徑並讀取CSV檔案

讀取CSV檔案

最近在研究如何讀取CSV檔案,試了一下原來並沒有想像中那麼難~
不過還沒達到本人的需求~繼續努力!!

01 using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Linq;
07 using System.Text;
08 using System.Windows.Forms;
09
10 using System.Data.OleDb;
11
12 namespace WindowsFormsApplication2
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }

20
21         private void Form1_Load(object sender, EventArgs e)
22         {
23             try
24             {
25                 using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\;Extended Properties='Text;'"))

26                 {
27                     DataTable dtTable = new DataTable();
28
29                     OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [BFI82U-20100105.csv]", conn);
30
31                     adapter.Fill(dtTable);
32
33                     this.dataGridView1.DataSource = dtTable;
34                 }

35
36             }

37             catch (DataException ex )
38             {
39                 ex.ToString();
40             }

41         }

42     }

43 }

44