티스토리 뷰

프로그래밍/C#

C# MySQL 로그인폼 만들기 #2

뽀로로친구에디 2020. 12. 29. 10:40

 

1. 레이아웃을 만들어줍니다. 

 

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

using MySql.Data.MySqlClient;
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;

namespace HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

      

        private void button_login_Click(object sender, EventArgs e)
        {
            try
            {
                // MySQL 데이타베이스를 연결하기 위해서는 MySqlConnection 클래스를 사용한다. 
                // 이 클래스를 생성할 때, Connection String을 넣어 주어야 하는데, 여기에는 datasource명, port번호, 사용자명, 암호을 지정해 준다.
                string myConnection = "datasource = localhost; port=3306; username=root; password=root";
                MySqlConnection myConn = new MySqlConnection(myConnection);

                // MySqlCommand에 해당 SQL문을 지정하여 실행한다
                MySqlCommand SelectCommand = new MySqlCommand("select * from test.student " +
                    "where id = '"+this.textBox_id.Text+"' and password = '"+this.textBox_pw.Text+ "';",myConn);

                // MySqlDataReader는 연결모드로 데이타를 서버에서 가져온다.
                MySqlDataReader myReader;
                myConn.Open();
                myReader = SelectCommand.ExecuteReader();
                int count = 0;
                
                while(myReader.Read())
                {
                    count = count + 1;
                }

                if(count==1)
                {
                    MessageBox.Show("아이디와 패스워드가 일치합니다.");
                }
                else if(count >1)
                {
                    MessageBox.Show("아이디와 패스워드가 중복됩니다.");
                }
                else
                {
                    MessageBox.Show("아이디와 패스워드가 불일치합니다.");
                }

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

 

youtu.be/KvdnN_54x5M

 

댓글
최근에 달린 댓글
글 보관함
«   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
    뽀로로친구에디
    최근에 올라온 글