Fly to the sky & Return

1. C#에서 mdb 파일을 연결해보자...... 본문

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

1. C#에서 mdb 파일을 연결해보자......

낼은어떻게 2015. 6. 4. 17:07
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.



Northwind.MDB


mdb는  엑세스에서 사용하는 파일입니데. 최근에는 확장자가 변경되었지만.. 머 요즘도 mdb 파일을 쓰는 곳들이 많아서.....  다른 sql서버관련 설치도 귀찬하고 하니....  그냥 mdb파일을 데이터베이스 파일로 사용하도록 하겠습니당.

예제파일은  MS sql 연습할때 사용되던 데이터베이스중에 northwind로 하겠습니다.



1
2
3
4
5
6
7
8
9
10
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
cs

mdb 파일을 사용하기 위해서 위에 9번 라인에 있는 'System.Data.OleDb '을 추가합니다.


다음으로는 데이터에 연결하고 DataSet 을 return 하는 Class를 만들도록 하겠습니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class DataConn
    {
        public DataSet GetDataset(string sql, string DB_path)
        {
            string connStr =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_path + ";Jet OLEDB:Database Password=";
            
 
            OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connStr);
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
            adp.Fill(ds);
            return ds; 
        }       
    }
cs

sql 문과 DB_path를 받아서 DataSet인 ds를 return하는 코드입니다.


자 이제 폼이 로드 될때 저 코드를 이용해보도록 하겠습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
 private void Form1_Load(object sender, EventArgs e)
        {
 
            DataConn conn1 = new DataConn();
            DataSet ds;
            string DB_path = targetPath;
 
            string sql = @"SELECT * from Customers";
 
            ds = conn1.GetDataset(sql, DB_path);
            dataGridView1.DataSource = ds.Tables[0];
        }
 
cs


targetPath가 정의가 되지 않았군여..


public으로 정의를 해봅니다.

1
2
3
4
5
6
7
        public string targetPath = Application.StartupPath + @"\Norwind.mdb";
 
        public Form1()
        {
            InitializeComponent();
        }
 
cs


이제 정해진 위치에 mdb 파일만 있다면  원하는 결과값이 폼위에 있는 datagridview에 나타날 것입니다..



참 데이터베이스 경로는....     프로젝트 저장위치에 프로젝트 네임안에 들어있는 Debug 폴더 안에 위치합니다.


이제 전체 코드를 보겠습니다.

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
 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
 
namespace brog_1
{
    public partial class Form1 : Form
    {
        public string targetPath = Application.StartupPath + @"\Norwind.mdb";
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
            DataConn conn1 = new DataConn();
            DataSet ds;
            string DB_path = targetPath;
 
 
            string sql = @"SELECT * from Customers";
 
            ds = conn1.GetDataset(sql, DB_path);
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
 
    class DataConn
    {
        public DataSet GetDataset(string sql, string DB_path)
        {
            string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_path + ";Jet OLEDB:Database Password=";
           
 
            OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connStr);
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
            adp.Fill(ds);
            return ds;
 
        }
 
        
    }
}
 
cs