Blog Content

    티스토리 뷰

    Window PDA 입력,출력,수정,삭제,검색 기능

    반응형

    초 간단하게 기능 구현~공부 하면서 제가 직접 작성했어요...




    using System;

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlServerCe;

    namespace SmartDeviceProject2
    {
        public partial class Form2 : Form
        {
            private DataSet dataset;
            private SqlCeDataAdapter adapter;
            private int cmdMode;

            public Form2()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string connStr = @"Data Source=\Program Files\SmartDeviceProject1\TestDB.sdf";

                SqlCeConnection conn = new SqlCeConnection(connStr);
                try
                {
                    dataset = new DataSet();
                    SqlCeCommand cmd = new SqlCeCommand();

                    //생성
                    cmd.CommandText = "Select Num, Name, Email From TBuser";
                    //Connection 객체 연결
                    cmd.Connection = conn;
                    conn.Open();

                    //SqlDataAdapter 클래스의 객체 생성
                    adapter = new SqlCeDataAdapter();
                    //SqlDataAdapter 클래스의 Command 모드 설정
                    adapter.SelectCommand = cmd;

                    //SqlDataadapter 클래스를 이용해서 DataSet 개체에 데이터를 채움
                    adapter.Fill(dataset, "TBuser");
                    //DataSet 개체에 담긴 데이터를 DataGrid 컨트롤에 바인딩 시킴
                    this.dataGrid1.DataSource = dataset.Tables["TBuser"];
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message.ToString());
                }
                finally
                {
                    try
                    {
                        if (conn != null)
                        {
                            conn.Close();
                        }
                    }
                    catch(Exception)
                    {
                    }
                }
            }

            #region 입력

            //입력 팝업창 표시
            private void button2_Click(object sender, EventArgs e)
            {
                cmdMode = 1;    //입력모드
                this.popUp();
                textBox1.Text = "";
            }

            //입력 팝업창 불러오기
            private void popUp()
            {
                panel1.Visible = true;
               
                //입력과 수정창이 화면에 보이도록 이동
                this.panel1.Top = 20;
                this.panel1.Left = 32;

                //보여지는 Panel을 맨 앞으로 지정
                this.panel1.BringToFront();
                this.textBox1.Focus();

                //수정버튼을 클릭 했다면...
                if (cmdMode == 2)
                {
                    //DataGrid 컨트롤에 현재 선택되어진 셀에 데이터를 TextBox 컨트롤에 표시
                    this.textBox1.Focus();

                    this.textBox1.Text = this.dataset.Tables["TBuser"].Rows[this.dataGrid1.CurrentRowIndex]["Name"].ToString();
                    this.textBox2.Text = this.dataset.Tables["TBuser"].Rows[this.dataGrid1.CurrentRowIndex]["Email"].ToString();
                    this.textBox3.Text = this.dataset.Tables["TBuser"].Rows[this.dataGrid1.CurrentRowIndex]["Num"].ToString();
                }
            }

            //팝업창 - 입력버튼
            private void button5_Click(object sender, EventArgs e)
            {
                DataRow dr = null;
                string columnName = "";

                if (cmdMode == 1)
                {
                    //dr = dataset.Tables["TBuser"].NewRow();

                    //columnName = dataset.Tables["TBuser"].Columns[1].ColumnName;
                    //dr[columnName] = this.textBox1.Text;

                    //columnName = dataset.Tables["TBuser"].Columns[2].ColumnName;
                    //dr[columnName] = this.textBox2.Text;

                    //dataset.Tables["TBuser"].Rows.Add(dr);

    //====================================================================================================================================
                    string connStr = @"Data Source=\Program Files\SmartDeviceProject1\TestDB.sdf";
                    SqlCeConnection conn = new SqlCeConnection(connStr);
                    try
                    {
                        dataset = new DataSet();
                        SqlCeCommand cmd = new SqlCeCommand();

                        //생성
                        cmd.CommandText = "Select Num, Name, Email From TBuser";
                        cmd.CommandText = "Insert Into TBuser(Name, Email) Values('" + textBox1.Text + "','" + textBox2.Text + "')";
                        //Connection 객체 연결
                        cmd.Connection = conn;
                        conn.Open();

                        //SqlDataAdapter 클래스의 객체 생성
                        adapter = new SqlCeDataAdapter();
                        //SqlDataAdapter 클래스의 Command 모드 설정
                        adapter.SelectCommand = cmd;

                        //SqlDataadapter 클래스를 이용해서 DataSet 개체에 데이터를 채움
                        adapter.Fill(dataset, "TBuser");
                        //DataSet 개체에 담긴 데이터를 DataGrid 컨트롤에 바인딩 시킴
                        this.dataGrid1.DataSource = dataset.Tables["TBuser"];

                        panel1.Hide();
                    }

                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message.ToString());
                    }
    //====================================================================================================================================
                }
                else if(cmdMode == 2)
                {
                    this.dataset.Tables["TBuser"].Rows[this.dataGrid1.CurrentRowIndex]["Name"] = textBox1.Text;
                    this.dataset.Tables["TBuser"].Rows[this.dataGrid1.CurrentRowIndex]["Email"] = textBox2.Text;

                    dataset = new DataSet();
                    SqlCeCommand cmd = new SqlCeCommand();
                    cmd.CommandText = "Update TBuser Set Name = '" + textBox1.Text + "', Email = '" + textBox2.Text + "' Where Num = " + textBox3.Text + "";
                    //cmd.CommandText = "Update TBuser Set Name = '" + textBox1.Text + "', Email = '" + textBox2.Text + "'";

                    saveData();

                    panel1.Hide();
                }
            }

            private void saveData()
            {
                string conStr = @"Data Source=\Program Files\SmartDeviceProject1\TestDB.sdf";
                SqlCeConnection con = new SqlCeConnection(conStr);
                try
                {
                    dataset = new DataSet();
                    SqlCeCommand cmd = new SqlCeCommand();

                    cmd.CommandText = "select Num, Name, Email From TBuser";
                    cmd.CommandText = "Update TBuser Set Name = '" + textBox1.Text + "', Email = '" + textBox2.Text + "'Where Num = " + textBox3.Text + "";
                    cmd.Connection = con;
                    con.Open();
                    adapter = new SqlCeDataAdapter();
                    adapter.SelectCommand = cmd;

                    adapter.Fill(dataset, "TBuser");
                    this.dataGrid1.DataSource = dataset.Tables["TBuser"];

                }
                catch(Exception err)
                {
                    MessageBox.Show(err.Message.ToString());
                }
            }

           
            //팝업창 - 취소버튼
            private void button6_Click(object sender, EventArgs e)
            {
                panel1.Hide();
            }

            #endregion

            //수정버튼
            private void button3_Click(object sender, EventArgs e)
            {
                cmdMode = 2;
                popUp();
            }

            //삭제버튼
            private void button4_Click(object sender, EventArgs e)
            {
                dataset.Tables["TBuser"].Rows[this.dataGrid1.CurrentCell.RowNumber].Delete();

                DisplayData();

            }

            private void DisplayData()
            {
                string conStr = @"Data Source=\Program Files\SmartDeviceProject1\TestDB.sdf";
                SqlCeConnection conn = new SqlCeConnection(conStr);

                try
                {
                    SqlCeCommandBuilder cb = new SqlCeCommandBuilder(adapter);
                    adapter.Update(dataset, "TBuser");
                    this.dataset.Tables["TBuser"].AcceptChanges();
                }

                catch (Exception err)
                {
                    MessageBox.Show(err.Message.ToString());
                }
                finally
                {
                    try
                    {
                        if (conn != null)
                        {
                            conn.Close();
                        }
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message.ToString());
                    }
                }
            }

            //검색버튼
            private void button7_Click(object sender, EventArgs e)
            {
                string conStr = @"Data Source=\Program Files\SmartDeviceProject1\TestDB.sdf";
                SqlCeConnection conn = new SqlCeConnection(conStr);

                try
                {
                    dataset = new DataSet();

                    SqlCeCommand cmd = new SqlCeCommand();
                    cmd.CommandText = "Select *From TBuser Where Name = '" + textBox4.Text + "'";

                    MessageBox.Show("너가 찾는게 " + textBox4.Text + " 맞음?");

                    cmd.Connection = conn;
                    conn.Open();

                    adapter = new SqlCeDataAdapter();
                    adapter.SelectCommand = cmd;


                    adapter.Fill(dataset, "TBuser");
                    this.dataGrid1.DataSource = dataset.Tables["TBuser"];

                }

                catch (Exception err)
                {
                    MessageBox.Show(err.Message.ToString());
                }
            }
        }
    }

    반응형

    'C#' 카테고리의 다른 글

    WPF RSS  (0) 2014.08.03
    [의뢰] openFileDialog 로 Text 텍스트 문서 읽어오기  (1) 2012.02.19
    Window CE로 연결  (0) 2010.09.02
    33.C#_WinForm - C# Setup.exe 만들기  (10) 2009.09.08
    32.C#_WinForm - C# 주소록 (3)  (2) 2009.09.03

    Comments