부품의 바코드값을 수정하

서병수·2023년 8월 5일
0

전진검사

목록 보기
6/6

부품의 이름으로 바코드를 조회한다.
조회한 바코드의 값을 수정할 수 있다.

using MySql.Data.MySqlClient;
using System;
using System.Windows.Forms;

namespace PREINSPECTION
{
    public partial class UpdatePart : Form
    {
        public UpdatePart()
        {
            InitializeComponent();
            using (MySqlConnection connection = ConnectDB.connectDB())
            {
                using (MySqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "Select name from part_group";
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        try
                        {
                            while (reader.Read())
                            {
                                partGroupSelect.Items.Add(reader[0].ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.writeLog(ex.ToString());
                        }
                    }
                }
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            if (partText.Enabled == false)
            {
                partText.Enabled = true;
            }
            else
            {
                string parttext = partText.Text;
                string partgrouptext;
                if (partGroupSelect.SelectedItem == null || parttext == "")
                {
                    MessageBox.Show("부품 그룹을 선택하거나 값을 입력하세요");
                    return;
                }
                else
                {
                    partgrouptext = partGroupSelect.SelectedItem.ToString();
                    using (MySqlConnection connection = ConnectDB.connectDB())
                    {
                        using (MySqlCommand command = connection.CreateCommand())
                        {

                            command.CommandText = "Select barcode " +
                                                  "from part " +
                                                  "where group_id = (select id from part_group where name = @partgrouptext) " +
                                                  "and name = @parttext";
                            command.Parameters.Add("@partgrouptext", MySqlDbType.VarChar).Value = partgrouptext;
                            command.Parameters.Add("@parttext", MySqlDbType.VarChar).Value = parttext;
                            try
                            {
                                object result = command.ExecuteScalar();
                                if (result != null)
                                {
                                    string barcode = result.ToString();
                                    barcodeText.Text = barcode;
                                    partText.Enabled = false;
                                }
                                else
                                {
                                    MessageBox.Show("이름이 일치하는 부품이 없습니다..");
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.writeLog(ex.ToString());
                            }
                        }
                    }
                }
            }
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            string barcodetext = barcodeText.Text.Trim();
            string parttext = partText.Text.Trim();
            if (partText.Enabled == true)
            {
                MessageBox.Show("부품을 다시 조회하세요");
            }
            else
            {
                using (MySqlConnection connection = ConnectDB.connectDB())
                {
                    using (MySqlCommand command = connection.CreateCommand())
                    {

                        command.CommandText = "update part " +
                                              "set barcode = @barcodetext " +
                                              "where name = @parttext";
                        command.Parameters.Add("@barcodetext", MySqlDbType.VarChar).Value = barcodetext;
                        command.Parameters.Add("@parttext", MySqlDbType.VarChar).Value = parttext;
                        try
                        {
                            int affected = command.ExecuteNonQuery();
                            if (affected != 0 )
                            {
                                MessageBox.Show("저장됐습니다.");
                            }
                            else
                            {
                                MessageBox.Show("저장에 실패했습니다.");
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.writeLog(ex.ToString());
                        }
                    }
                }
            }
        }
    }
}
profile
안녕하십니까 인사올립니다

0개의 댓글