제품 조회하기

서병수·2023년 7월 31일
0

전진검사

목록 보기
4/6

23년 7월 31일

오늘은 DB에 접속해서 제품을 불러오는 폼을 작성했다.

   public partial class itemSelect : Form
    {
        public itemSelect()
        {
            InitializeComponent();
        }

        private void itemSelect_Load(object sender, EventArgs e)
        {
            using (MySqlConnection connection =  ConnectDB.connectDB())
            {
                using (MySqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "Select name from item_group";
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        try
                        {
                            while (reader.Read())
                            {
                                itemGroupBox.Items.Add(reader[0].ToString());
                            }
                        }
                        catch(Exception ex)
                        {
                            Log.writeLog(ex.ToString());
                        }
                    }
                }
            }
        }
        private void itemGroupBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            itemBox.Items.Clear();
            string selectedItem=itemGroupBox.SelectedItem.ToString();
            using (MySqlConnection connection = ConnectDB.connectDB())
            {
                using (MySqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = @"select item.name from item_group inner join " +
                        "item on item_group.id= item.group_id " +
                        "where item_group.name = @SelectedItem";
                    command.Parameters.Add("@SelectedItem", MySqlDbType.VarChar,45).Value= selectedItem;
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        try
                        {
                            while (reader.Read())
                            {
                                itemBox.Items.Add(reader[0].ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.writeLog(ex.ToString());
                        }
                    }
                }
            }

        }
    }


폼이 로드될 때 왼쪽의 ComboBox에 값을 채워넣고 왼쪽 ComboBox를 선택하면 오른쪽의 ComboBox의 값이
채워진다.

profile
안녕하십니까 인사올립니다

1개의 댓글

comment-user-thumbnail
2023년 7월 31일

좋은 정보 감사합니다

답글 달기
Powered by GraphCDN, the GraphQL CDN