Fly to the sky & Return

TEXT file - > DataSet 본문

프로그래밍/c# 윈폼 데이터베이스 기초부터

TEXT file - > DataSet

낼은어떻게 2021. 12. 25. 19:43
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
 
 
 
namespace txt_data_test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private DataSet GetDataFile(string fileName)
        {
            DataSet ds = new DataSet();
 
            DataTable table = new DataTable();
 
            table.Columns.Add("Co1");
            table.Columns.Add("Co2");
            table.Columns.Add("Co3");
            table.Columns.Add("Co4");
            table.Columns.Add("Co5");
 
            try
            {              
                using (StreamReader reader = new StreamReader(fileName, Encoding.Default))
                {
                    while (reader.Peek() >= 0)
                    {
 
                        DataRow newRow = table.NewRow();
                        string[] datas = reader.ReadLine().Split(';');
 
                        for (int col = 0; col < datas.Length; col++)
                        {
                            newRow[col] = datas[col];
                        }
 
                        table.Rows.Add(newRow);
                        datas = null;
                        newRow = null;
                    }
 
                    ds.Tables.Add(table);                  
                }           
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
 
            return ds;
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
            }
 
            DataSet ds = GetDataFile(fileName);
 
            dataGridView1.DataSource = ds.Tables[0];
 
        }
    }
 
   
}
cs