티스토리 뷰

C# MySQL Update 명령문 사용해서 수정하기 

 

1. 폼을 다음과 같이 구성합니다. 

 

2. 소스코드를 입력합니다. 

 private void button_update_Click(object sender, EventArgs e)
        {
            // MySQL 데이타베이스를 연결하기 위해서는 MySqlConnection 클래스를 사용한다. 
            // 이 클래스를 생성할 때, Connection String을 넣어 주어야 하는데, 여기에는 datasource명, port번호, 사용자명, 암호을 지정해 준다.
            string myConnection = "datasource = localhost; port=3306; username=root; password=root";
            string Query = "update test.student set no = '" + this.tb_no.Text + "', name = '" + this.tb_name.Text + "'," +
                "age ='" + this.tb_age.Text + "', id = '" + this.tb_id.Text + "',password = '" + this.tb_pw.Text + "'" +
                " where no = '" + this.tb_no.Text + "';";

            MySqlConnection myConn = new MySqlConnection(myConnection);

            // MySqlCommand에 해당 SQL문을 지정하여 실행한다
            MySqlCommand SelectCommand = new MySqlCommand(Query, myConn);

            // MySqlDataReader는 연결모드로 데이타를 서버에서 가져온다.
            MySqlDataReader myReader;

            try
            {
                myConn.Open();

                //ExecuteReader를 이용하여 연결 모드로 데이타 가져오기
                myReader = SelectCommand.ExecuteReader();
                MessageBox.Show("수정됨");

                while (myReader.Read())
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

 

3. 수정이 잘되는지 확인합니다. 

youtu.be/D6EQ_ND31GQ

 

댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글