반응형

현재 텍스트파일의 내용들을 데이터베이스인 엑세스파일에 데이터를 넣어주는 소스입니다.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
 
namespace DB_1
{
    class Program
    {
 
        static void Main(string[] args)
        {
            OleDbConnection myConnection = null;
            OleDbCommand myCommand = null;
 
            myConnection = new OleDbConnection();
            myCommand = new OleDbCommand();
 
            try
            {
                //db 연결하는 부분
                myConnection.ConnectionString =
                    "Provider=Microsoft.ACE.OLEDB.12.0; " +
                    "Data Source=../../../DBfile/WORKER_DB.accdb; " +
                    "Persist Security Info=False";
 
                myConnection.Open();
 
                //컴멘드 객체에 연결된 연결 객체를 대입
                myCommand.Connection = myConnection;
 
                // /////////////////////////////////////////////
                //우편번호가 있는 텍스트 파일 열기
                System.IO.StreamReader DBfile =
                    new System.IO.StreamReader("../../../DBfile/TEST_UTF8.txt");
 
                string zipStr;
                long count = 0;
 
                //현재의 테이블 내용을 삭제. 왜냐하면 테이블에 주키가 없기
                //때문에 삭제하지 않으면 이 프로그램을 실행 할 때마다
                //같은 데이터가 추가되기 때문이다.
                myCommand.CommandText = "delete from WORKER";
                myCommand.ExecuteNonQuery();
 
                //비어 있는 DB 테이블에 텍스트 파일의 모든 내용을
                //테이블에 추가
                while (!DBfile.EndOfStream)
                {
                    //텍스트 파일에서 한 줄의 내용을 읽기
                    zipStr = DBfile.ReadLine();
 
                    //tab키를 구분자로 사용하여 문자열을 여러개의
                    //작은 문자열로 분리
                    string[] words = zipStr.Split('\t');
 
                    string query =
                        "insert into WORKER (NM, NUM, BU, LEV, BUM, TEL, INTEL, JUSO) ";
 
                    query += "values (";
                    query += "'" + words[0] + "', ";
                    query += "'" + words[1] + "', ";
                    query += "'" + words[2] + "', ";
                    query += "'" + words[3] + "', ";
                    query += "'" + words[4] + "', ";
                    query += "'" + words[5] + "', ";
                    query += "'" + words[6] + "', ";
                    query += "'" + words[7] + "'";
                    query += ")";
 
                    //Console.WriteLine(query);
 
                    //명령객체의 CommandText에 쿼리 문자열을 대입
                    myCommand.CommandText = query;
 
                    //쿼리를 실행
                    myCommand.ExecuteNonQuery();
 
                    count++;
 
                    if ((count % 1) == 0) Console.WriteLine(count);
                }//while
 
                //텍스트 파일 닫기
                DBfile.Close();
 
                //DB 연결 끊기
                myConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
 
        }//Main
    }
}

+ Recent posts