Blog Content

    티스토리 뷰

    25.ADO.NET - DataAdapter

    반응형
    DataAdapter : DataSet을 체우고 데이터 소스를 업데이트 하는데 사용되는 SQL 명령 집합 및
                데이터 베이스 연결

    Form1.Designer.cs

    [그림25-1]


           private System.Windows.Forms.DataGridView dataGridView1;

           private System.Windows.Forms.DataGridView dataGridView2;

           private System.Windows.Forms.Button btnSave;




    Form1.cs

    namespace DataAdapter

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                DisplayData1();

                DisplayData2();

            }

     

            private SqlDataAdapter da2;

            private DataTable dt2;

            private void DisplayData2()

            {

                SqlConnection con = new SqlConnection(

                    "server=WINDOWS-XP\\SQLSERVER;database=Test;uid=Test;pwd=1234;");

     

                SqlCommand cmd = new SqlCommand("Select * From Categories", con);          

     

                da2 = new SqlDataAdapter(cmd); // Command(명령) 여러개 담을 있는 그릇

                da2.SelectCommand = cmd;

     

                cmd = new SqlCommand(

                    "Insert Into Categories Values(@CategoryName, @SuperCategory, @Align)", con);

     

                cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 25, "CategoryName");

                cmd.Parameters.Add("@SuperCategory", SqlDbType.Int, 10, "SuperCategory");

                cmd.Parameters.Add("@Align", SqlDbType.Int, 10, "Align");

                da2.InsertCommand = cmd;

     

     

     

                cmd = new SqlCommand

                    ("Update Categories Set CategoryName = @CategoryName, SuperCategory =

                 @SuperCategory, Align = @Align Where Num = @Num", con);

                cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 25, "CategoryName");

                cmd.Parameters.Add("@SuperCategory", SqlDbType.Int, 10, "SuperCategory");

                cmd.Parameters.Add("@Align", SqlDbType.Int, 10, "Align");

                cmd.Parameters.Add("@CategoryID", SqlDbType.Int, 10, "CategoryID");

                da2.UpdateCommand = cmd;

               

                cmd = new SqlCommand("Dalete From Categories Where Num = @Num", con);

                cmd.Parameters.Add("@Num", SqlDbType.Int, 10, "Num");

                da2.DeleteCommand = cmd;

     

                dt2 = new DataTable("Categories");

                da2.Fill(dt2);

                this.dataGridView2.DataSource = dt2;

            }

     

            private void DisplayData1()

            {

                SqlConnection con = new SqlConnection();

                con.ConnectionString =

                    "server=WINDOWS-XP\\SQLSERVER;database=Test;uid=Test;pwd=1234;";

                con.Open();

     

                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "Select * From Categories";

                cmd.Connection = con;

               

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

                DataTable dt = new DataTable();

                dt.Load(dr);

     

                // DataGridView 속성 ReadOnly 속성 true

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

               

                dr.Close();

                con.Close();

            }

     

            private void btnSave_Click(object sender, EventArgs e)

            {

                da2.Update(dt2); //변경 내용 적용

            }

        }

    }

     


    결과화면

    [그림25-2]


    수정 삭제 됩니다~


    반응형

    'ASP.NET' 카테고리의 다른 글

    27.ADO.NET - Binding  (0) 2009.09.30
    26.ADO.NET - SqlCommandBuilder  (0) 2009.09.30
    24.ADO.NET - CommandTest  (0) 2009.09.30
    23.ADO.NET - ConnectionEvent  (0) 2009.09.30
    -- DataRelation (Xml 변환)  (0) 2009.09.30

    Comments