Presentation is loading. Please wait.

Presentation is loading. Please wait.

DB 프로그래밍1 2017.1학기.

Similar presentations


Presentation on theme: "DB 프로그래밍1 2017.1학기."— Presentation transcript:

1 DB 프로그래밍1 2017.1학기

2 Create Read Update Delete ACCESS Oracle MySQL MS SQLServer … OleDB
OleDBConnection OleDBCommand OleDBDataReader Database Program Create Read Update Delete Record Table Field

3 만들고자 하는 프로그램 ListBox 추가, 검색, 수정, 삭제가 가능한 DB 프로그래밍

4 01 Access DB 작성

5 02 폼 디자인 txtID txt학번 txt이름 txt전화번호

6 03 Data Source 연결(Access DB 연결)
보기다른 창데이터소스 Data Sources 메뉴 Add New Data Source Database 선택 Dataset 선택

7 New Connection MS Access Database File Browse Test Connection

8 04 Connection String ConnectionStrings.com

9 참고: DB프로그래밍의 원칙 OleDB OleDbConnection conn = null; // connection
OleDbCommand comm = null; // command OleDbDataReader reader = null; // DataReader 또는 // ExecuteNonQuery string connStr = null; connStr을 connectionstrings.com 에서 가져온다

10 SQL 문 SELECT * FROM 테이블이름 SELECT * FROM 테이블이름 WHERE SID=…
UPDATE 테이블이름 SET ID=…, SID=…, Sname=…, Phone=… WHERE ID=… 이때 숫자는 따옴표 없이, 문자열은 ‘문자열’ 처럼 작은 따옴표를 넣어주어야 한다 INSERT INTO 테이블이름 VALUES ( … , … , … , …) DELETE FROM 테이블이름 WHERE ID=…

11 04 displayStudents() –DB 내용을 모두 읽어와서 리스트박스에 표시함
using System.Data.OleDb; … OleDbConnection conn = null; OleDbCommand comm = null; OleDbDataReader reader = null; string connStr = null; public Form1() { InitializeComponent(); DisplayStudents(); } private void DisplayStudents() { if(conn == null) // DB 연결 후 Open connStr Source=…."; conn = new OleDbConnection(connStr); conn.Open(); // SQL 명령어 만들고 string sql = "SELECT * FROM StudentTable"; // 실행 comm = new OleDbCommand(sql, conn); reader = comm.ExecuteReader(); // 여러개 찾으면 각각에 대해서 루프에서 처리 while(reader.Read()) string item = ""; item += reader["ID"].ToString() + "\t"; item += reader["SID"].ToString() + "\t"; item += reader["SName"].ToString() + "\t"; item += reader["Phone"].ToString(); listBox1.Items.Add(item); } reader.Close(); conn.Close(); conn = null;

12 05 listbox1_Click - 리스트박스에서 데이터를 클릭하면 해당 값을 텍스트박스에 표시함
private void listBox1_Click(object sender, EventArgs e) { ListBox lb = sender as ListBox; if (lb.SelectedItem == null) return; string[] s = lb.SelectedItem.ToString().Split('\t'); txtID.Text = s[0]; txt학번.Text = s[1]; txt이름.Text = s[2]; txt전화번호.Text = s[3]; }

13 06 INSERT INTO 테이블 VALUES - 텍스트박스에 데이터를 넣고 DB에 추가
// 추가 private void button2_Click(object sender, EventArgs e) { if (txt이름.Text == "" || txt학번.Text == "" || txt전화번호.Text == "") return; // 다시 DB를 연결하고 Open 한다 conn = new OleDbConnection(connStr); conn.Open(); // SQL문장으로 명령어를 만든다 string sql = "INSERT INTO StudentTable VALUES (" + txtID.Text + ", '" + txt학번.Text + "', '" + txt이름.Text + "', '" + txt전화번호.Text + "')"; // 명령어를 실행한다 comm = new OleDbCommand(sql, conn); int x = comm.ExecuteNonQuery(); if (x == 1) MessageBox.Show("정상 삽입"); conn.Close(); conn = null; listBox1.Items.Clear(); DisplayStudents(); }

14 07 DELETE FROM 테이블 WHERE - 텍스트 상자에 있는 ID 값으로 DB에서 삭제
// 삭제 private void button5_Click(object sender, EventArgs e) { if (txtID.Text == "") return; conn = new OleDbConnection(connStr); conn.Open(); string sql = "DELETE FROM StudentTable WHERE ID=" + txtID.Text; comm = new OleDbCommand(sql, conn); int x = comm.ExecuteNonQuery(); if (x == 1) MessageBox.Show("정상적으로 삭제되었습니다"); conn.Close(); conn = null; listBox1.Items.Clear(); DisplayStudents(); }

15 08 UPDATE 테이블 SET ~ WHERE - ID로 DB를 검색하여 값을 수정
// 수정 private void button6_Click(object sender, EventArgs e) { if (txtID.Text == "") return; conn = new OleDbConnection(connStr); conn.Open(); string sql = "UPDATE StudentTable SET SID='" + txt학번.Text + "', SName='" + txt이름.Text + "', Phone='" + txt전화번호.Text + "' WHERE ID=" + txtID.Text; MessageBox.Show(sql); comm = new OleDbCommand(sql, conn); int x = comm.ExecuteNonQuery(); if (x == 1) MessageBox.Show("정상적으로 수정되었습니다"); conn.Close(); conn = null; listBox1.Items.Clear(); DisplayStudents(); }

16 09 SELECT * FROM 테이블 WHERE ~ - 학번, 이름, 전화번호로 검색하여 리스트 박스에 표시
private void button3_Click(object sender, EventArgs e) { // 4개 텍스트박스 중 적어도 하나는 찾을 값이 있어야 한다 if (txtID.Text == "" && txt학번.Text == "" && txt이름.Text == "" && txt전화번호.Text == "") { MessageBox.Show("텍스트 상자 중 하나에 검색할 자료를 입력하세요"); return; } string sql = ""; conn = new OleDbConnection(connStr); conn.Open(); if (txt학번.Text != "") { sql = "SELECT * FROM StudentTable WHERE SID='" + txt학번.Text + "'";} else if (txt이름.Text != "") { sql = "SELECT * FROM StudentTable WHERE SName='" + txt이름.Text + "'";} else if (txt전화번호.Text != "") { sql = "SELECT * FROM StudentTable WHERE Phone='" + txt전화번호.Text + "'"; } comm = new OleDbCommand(sql, conn); reader = comm.ExecuteReader(); listBox1.Items.Clear(); while (reader.Read()) string item = ""; item += reader["ID"].ToString() + "\t"; item += reader["SID"].ToString() + "\t"; item += reader["SName"].ToString() + "\t"; item += reader["Phone"].ToString(); listBox1.Items.Add(item); } reader.Close(); conn.Close(); conn = null;

17 Visual Studio


Download ppt "DB 프로그래밍1 2017.1학기."

Similar presentations


Ads by Google