Busan IT/제어 UI(C#)2015. 6. 29. 13:16


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace _20150626
{
    public partial class Form1 : Form
    {
        private string StrSQL = "Data Source=localhost;Database=test;User Id=root;Password=root;";
        //user id=root;persistsecurityinfo=True;server=localhost;database=test
        DataTable tb_myfriend;
        DataSet ds;
        string Current_Name;
        public Form1()
        {
            InitializeComponent();
        }
        private bool Control_Check()
        {
            if ((tb_Name.Text != ""&& (tb_Phone.Text != "" || tb_Mobile.Text != ""))
                return true;
            else
            {
                MessageBox.Show("이름과 전화번호는 반드시 입력되어야합니다.""에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private bool Overlap_Check(string Std)
        {
            foreach (string item in lb_Box.Items)
            {
                if (item == Std)
                    return false;
            }

            return true;
        }

        void Control_Clear() // 입력란 공백 처리
        {
            this.tb_Name.Clear();
            this.tb_Email.Clear();
            this.tb_Phone.Clear();
            this.tb_Address.Clear();
            this.tb_Company.Clear();
            this.tb_Department.Clear();
            this.tb_Introduce.Clear();            
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'testDataSet1.myfriend' table. You can move, or remove it, as needed.
            this.myfriendTableAdapter1.Fill(this.testDataSet1.myfriend);


            var conn = new MySqlConnection(StrSQL);
            conn.Open();
            var MySqlAdapter = new MySqlDataAdapter("select * from myfriend", conn);

            ds = new DataSet();

            tb_myfriend = ds.Tables.Add("fTable");
            MySqlAdapter.Fill(ds, "fTable");

            conn.Close();

            DataColumn[] pk = new DataColumn[1];
            pk[0= tb_myfriend.Columns["Name"];

            tb_myfriend.PrimaryKey = pk;
            dataGridView1.DataSource = tb_myfriend;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridView1.ReadOnly = true;
        }
        private void lb_Box_Update()
        {           
            var MConn = new MySqlConnection(StrSQL);
            MConn.Open();
            var Comm = new MySqlCommand("Select * from myfriend", MConn);
            var MyRead = Comm.ExecuteReader();

            while (MyRead.Read())
            {
                var strArray = new String[] {MyRead[0].ToString(),
                MyRead[1].ToString(), MyRead[2].ToString(),
                MyRead[3].ToString(), MyRead[4].ToString(),
                MyRead[5].ToString(), MyRead[6].ToString(),
                MyRead[7].ToString(), MyRead[8].ToString()};

                tb_Name.Text = MyRead[1].ToString();
                tb_Email.Text = MyRead[2].ToString();
                tb_Phone.Text = MyRead[3].ToString();
                tb_Mobile.Text = MyRead[4].ToString();
                tb_Address.Text = MyRead[5].ToString();
                tb_Company.Text = MyRead[6].ToString();
                tb_Department.Text = MyRead[7].ToString();
                tb_Introduce.Text = MyRead[8].ToString();
            }
            MyRead.Close();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {            
            string name = tb_Name.Text.Trim();
            string email = tb_Email.Text.Trim();
            string phone = tb_Phone.Text.Trim();
            string mobile = tb_Mobile.Text.Trim();
            string address = tb_Address.Text.Trim();
            string company = tb_Company.Text.Trim();
            string department = tb_Department.Text.Trim();
            string introduce = tb_Introduce.Text.Trim();

            var conn = new MySqlConnection(StrSQL);
            conn.Open();

            var dt = tb_myfriend.NewRow();

            DataRow row = dt;
            row[1= tb_Name.Text;
            row[2= tb_Email.Text;
            row[3= tb_Phone.Text;
            row[4= tb_Mobile.Text;
            row[5= tb_Address.Text;
            row[6= tb_Company.Text;
            row[7= tb_Department.Text;
            row[8= tb_Introduce.Text;

            tb_myfriend.Rows.Add(row);

            var MySqlAdapter = new MySqlDataAdapter("select * from myfriend", conn);
            var cmdBuild = new MySqlCommandBuilder(MySqlAdapter);
            MySqlAdapter.InsertCommand = cmdBuild.GetInsertCommand();
            MySqlAdapter.Update(ds, "fTable");
            MySqlAdapter.Fill(ds, tb_myfriend.Rows.Count - 1, tb_myfriend.Rows.Count - 1"fTable");
            cmdBuild.Dispose();
            conn.Close();
            Control_Clear();
            MessageBox.Show("저장되었습니다.""알림", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        private void btn_Search_Click(object sender, EventArgs e)
        {
            if (tb_Search.Text != "")
            {
                string Target = tb_Search.Text;

                DataRow row = tb_myfriend.Rows.Find(Target);

                tb_Search.Clear();

                if (row == null)
                {
                    MessageBox.Show("찾는 항목이 없습니다""에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (Overlap_Check(Target))
                    {
                        lb_Box.Items.Add(Target);
                    }
                    else
                    {
                        MessageBox.Show("이미 존재하는 항목입니다""에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

        }


        private void btn_Modify_Click(object sender, EventArgs e)
        {
            if (lb_Box.SelectedItem != null)
            {
                if (Control_Check())
                {
                    var conn = new MySqlConnection(StrSQL);
                    conn.Open();

                    var dt = ds.Tables["fTable"].Select("Name = '" + Current_Name + "'");

                    DataRow row = dt[0];
                    row[1= tb_Name.Text;
                    row[2= tb_Email.Text;
                    row[3= tb_Phone.Text;
                    row[4= tb_Mobile.Text;
                    row[5= tb_Address.Text;
                    row[6= tb_Company.Text;
                    row[7= tb_Department.Text;
                    row[8= tb_Introduce.Text;

                    var MySqlAdapter = new MySqlDataAdapter("select * from myfriend", conn);
                    var cmdBuild = new MySqlCommandBuilder(MySqlAdapter);
                    MySqlAdapter.UpdateCommand = cmdBuild.GetUpdateCommand();
                    MySqlAdapter.Update(ds, "fTable");
                    cmdBuild.Dispose();
                    conn.Close();

                    MessageBox.Show("수정되었습니다.""알림", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("선택된 항목이 없습니다.""에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (lb_Box.SelectedItem != null)
            {
                var Conn = new MySqlConnection(StrSQL);
                Conn.Open();

                var dt = ds.Tables["fTable"].Select("Name = '" + Current_Name + "'");

                dt[0].Delete();

                var MySqlAdapter = new MySqlDataAdapter("select * from myfriend", Conn);
                var cmdBuild = new MySqlCommandBuilder(MySqlAdapter);
                MySqlAdapter.DeleteCommand = cmdBuild.GetDeleteCommand();
                MySqlAdapter.Update(ds, "fTable");
                cmdBuild.Dispose();
                Conn.Close();

                lb_Box.Items.RemoveAt(lb_Box.SelectedIndex);
                Control_Clear();
                MessageBox.Show("삭제되었습니다.""알림", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("선택된 항목이 없습니다.""에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void lb_Box_Click(object sender, EventArgs e)
        {
            if (lb_Box.SelectedItem != null)
            {
                lb_Box_Update();
            }

        }

        private void btn_Prev_Click(object sender, EventArgs e)
        {
            if (lb_Box.SelectedIndex > 0)
            {
                lb_Box.SelectedIndex = lb_Box.SelectedIndex - 1;
                lb_Box_Update();
            }
        }

        private void btn_Next_Click(object sender, EventArgs e)
        {
            if (lb_Box.SelectedIndex < lb_Box.Items.Count - 1)
            {
                lb_Box.SelectedIndex = lb_Box.SelectedIndex + 1;
                lb_Box_Update();
            }
        }

        private void lb_Box_SelectedIndexChanged(object sender, EventArgs e)
        {           
            
        }

    }

}



** MSSql을 다운 받아서 설치해 놓기


반응형

'Busan IT > 제어 UI(C#)' 카테고리의 다른 글

프로세서와 스레드  (0) 2015.07.02
MSSQL  (0) 2015.06.30
ADO.NET, MySql과 자료 연동하여 학생 명부 만들기  (0) 2015.06.26
DataTable과 MySQL 연동  (0) 2015.06.25
MySql 데이터 연동  (0) 2015.06.24
Posted by newind2000