728x90

콤보박스에 뿌려질 category 리스트를 MySQL 데이터베이스와 연동하는 방법을 해봤다.

정말 힘들게 겨우 구현에 성공했다.

comboBox에 뿌려질 리스트를 만드는 것은 어렵지 않다.

문제는 값을 선택했을 때, Event 가 발생하도록 연동처리하는 것을 아직 완벽하게 이해하지 못했다.

uid 값을 한번에 넘기는 방법이 있을거 같은데 ㅠㅠㅠ

C# 배운지 한달도 안되는데 이 정도 구현한 것만으로도 나자신을 대견하게 생각해야 하겠지.

무식하게 2단계로 나눠서 comboBox1을 선택하면 cat1name 을 받아서 cat1uid 를 다시 조회하는 Query 문을 만든다음에 data 테이블로 결과를 던져서 원하는 결과를 가져오는 로직으로 구현을 했다.


void cat1comboBox()
{
    comboBox1.Items.Clear();
    comboBox1.Items.Add("전체");
    try
    {
        myConn = new MySqlConnection(ConnectionString);
        myConn.Open();
        string strqry = "select uid, name from category where relateduid=0";
        strqry += " order by uid";
        adapter = new MySqlDataAdapter(strqry, myConn);
        DataSet cat1 = new DataSet();
        adapter.Fill(cat1);
        foreach (DataRow dr in cat1.Tables[0].Rows)
        {
            comboBox1.Items.Add(dr["name"]);
        }

        comboBox1.SelectedIndex = 0;  // 첫번째 아이템 선텍

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        this.myConn.Close();
    }
}

DataTable cat2comboBox(string category1)
{
    comboBox2.Items.Clear();
    comboBox2.Items.Add("전체");
    try
    {
        myConn = new MySqlConnection(ConnectionString);
        myConn.Open();
        string strqry = "select uid, name from category";
        strqry += " where relateduid=(select uid from category where relateduid=0 and name='" + category1 + "')";
        strqry += " order by uid";
        adapter = new MySqlDataAdapter(strqry, myConn);
        DataSet cat2 = new DataSet();
        adapter.Fill(cat2);
        foreach (DataRow dr in cat2.Tables[0].Rows)
        {
            comboBox2.Items.Add(dr["name"]);
        }
        return cat2.Tables[0];
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return null;
    }
    finally
    {
        this.myConn.Close();
    }
}


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    cat2 = cat2comboBox(comboBox1.SelectedItem.ToString());
    cat1name = comboBox1.SelectedItem.ToString(); // data 테이블 검색어 전달

    myConn = new MySqlConnection(ConnectionString);
    myConn.Open();
    string strqry = "select uid from category";
    strqry += " where relateduid=0 and name='" + cat1name + "' ";
    MySqlCommand cmd = new MySqlCommand(strqry, myConn);
    cmd.CommandType = CommandType.Text;
    MySqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        cat1uid = dr.GetString(0);
    }
    dr.Close();
    myConn.Close();
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    cat2name = comboBox2.SelectedItem.ToString(); // data 테이블 검색어 전달
    myConn = new MySqlConnection(ConnectionString);
    myConn.Open();
    string strqry = "select uid from category";
    strqry += " where relateduid='" + cat1uid + "' and name='" + cat2name + "' ";
    MySqlCommand cmd = new MySqlCommand(strqry, myConn);
    cmd.CommandType = CommandType.Text;
    MySqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        cat2uid = dr.GetString(0);
    }
    dr.Close();
    myConn.Close();
}


-------------------------------------------------------------------------

함수를 더 정교하게 사용하는 방법은

comboBox1 부분을 CB 로 변경하는 것이다.

즉 comboBox CB 로 함수 인자를 만들어서 받으면 된다.



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

C# MySQL 간단 코드 및 접속 기본지식 배우기  (0) 2015.12.04
C# MySQL 쿼리문과 연동 처리  (0) 2015.09.12
C# 과 MySQL 연동  (0) 2015.08.29
C# ExecuteScalar 와 총 Record 수  (0) 2015.08.27
C# 과 MySQL 연결을 위한 준비  (0) 2015.08.05
블로그 이미지

Link2Me

,