블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

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
반응형

Category

Recent Post

Recent Comment

Archive

2009. 9. 30. 15:30 .Net Project/ADO.NET 3.5
반응형

 

DataAdapter
- 데이터 소스에서 데이터를 검색하고 DataSet 내의 테이블을 채우는 데 사용
- DataAdapter은 DataSet에 대한 변경 내용을 다시 데이터 소스에 적용
- DataAdapter는 닷넷 프레임 워크 데이터 공급자의 Connection개체를 사용하여 
  데이터 소스에 연결하여 Command개체를 사용하여 데이터 소스에서 데이터를 검색하고
  변경 내용을 데이터 소스에 적용합니다. 

 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WinDataAdapter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Display(); //DataReader이용
            Display1(); //DataSet - Fill 이용
        }
        //Connection - command - DataReader - DataSet
        // DataTable 윈폼만... 패턴 출력

        private void Display()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = 
           "server=.;database=AddressBook;uid=AddressBook;pwd=1234;";
            con.Open();
                       
            SqlCommand cmd = new SqlCommand
                              ("Select *From AddressBook", con);
            cmd.CommandType = CommandType.Text;

        SqlDataReader dr = cmd.ExecuteReader();//팩터리 메서드로 리더생성
            
        DataTable dt = new DataTable();
        dt.Load(dr);

            this.dataGridView1.DataSource = dt; //출력

            dr.Close();
            con.Close();
        }


        //Connection - Command - DataSet - Fill 패턴 출력
        private SqlDataAdapter da2;//필드레벨로 재선언
        private DataTable dt2;

        private void Display1()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString =            
            server=.;database=AddressBook;uid=AddressBook;pwd=1234;";
            con.Open();

            SqlCommand cmd = new SqlCommand
                       
            da2 = new SqlDataAdapter(cmd);
            da2.SelectCommand = cmd;
            
      //삽입
      cmd = new SqlCommand
      ("Insert Into AddressBook Values
                      (@Name, @Mobile, @Email)", con);
        cmd.Parameters.Add("@Name", SqlDbType.VarChar, 25, "Name");
        cmd.Parameters.Add
                              ("@Mobile",SqlDbType.VarChar, 25, "Mobile");
        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 25, "Email");
       
        da2.InsertCommand = cmd;

            //수정
           cmd = new SqlCommand
            ("Update AddressBook Set
               Name = @Name,
Mobile=@Mobile,
               Email=@Email
Where Num = @Num", con);
           cmd.Parameters.Add
               ("@Name", SqlDbType.VarChar, 25, "Name");
           cmd.Parameters.Add
               ("@Mobile", SqlDbType.VarChar, 25,"Mobile");
           cmd.Parameters.Add
                  ("@Email", SqlDbType.VarChar, 25,"Email");
            cmd.Parameters.Add("@Num", SqlDbType.Int, 32, "Num");

            da2.UpdateCommand = cmd;
           
            //삭제
            cmd = new SqlCommand
                     ("Delete AddressBook Where Num = @Num",con);
            cmd.Parameters.Add("@Num",SqlDbType.Int,32,"Num");
            da2.DeleteCommand = cmd;
       
            dt2 = new DataTable("Addressbook");
            da2.Fill(dt2);
                                           
            this.dataGridView2.DataSource = dt2;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            da2.Update(dt2); //변경 내역 적용하기
        }
    }
}








반응형
posted by Magic_kit