๋ฐ˜์‘ํ˜•

์†Œ๊ฐœ

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ๋Š” ์œˆ๋„์šฐ10์—์„œ MySQL์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ์†Œ๊ฐœํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค] ์œˆ๋„์šฐ10์— MySQL ์„ค์น˜ ๋ฐ ํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ

 

[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค] ์œˆ๋„์šฐ10์— MySQL ์„ค์น˜ ๋ฐ ํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์œ„ํ•œ MySQL MySQL์€ ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ(RDBMS; Relational Database Management System)์ž…๋‹ˆ๋‹ค. MySQL์€ ๋ฌด๋ฃŒ ๋ฒ„์ „์ธ ์ปค๋ฎค๋‹ˆํ‹ฐ ์—๋””์…˜(Community Edition)..

luckygg.tistory.com

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ฝ”๋“œ ๋ ˆ๋ฒจ์—์„œ ์–ด๋–ป๊ฒŒ MySQL์— ์ ‘๊ทผํ•˜์—ฌ DB์— ์ ‘๊ทผํ•˜๋Š”์ง€ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ๋ชฉํ‘œ

๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ MySQL์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „ํ™”๋ฒˆํ˜ธ๋ถ€๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋™์ž‘์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ๋™์ž‘

์˜ˆ์ œ ํ”„๋กœ์ ํŠธ๋Š” DB์— ์—ฐ๋™ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…(INSERT), ์ˆ˜์ •(UPDATE), ์‚ญ์ œ(DELETE), ์กฐํšŒ(SELECT) ๋™์ž‘์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ์ฝ”๋“œ์— ์‚ฌ์šฉ๋œ DB๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

DB ์˜ˆ์‹œ

accounts_table ์ด๋ฆ„์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ , Column์œผ๋กœ id(INT)์™€ name(VARCHAR) ๊ทธ๋ฆฌ๊ณ  phone(VARCHAR)์„ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. name์—๋Š” ์ด๋ฆ„์ด, phone์—๋Š” ์ „ํ™”๋ฒˆํ˜ธ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.

.Net C#์—์„œ MySQL์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ค€๋น„

ExamMySQL์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ C# WinForm ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋ˆ„๊ฒŸ(NuGet)์—์„œ MySql.Data ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

๋ˆ„๊ฒŸ์—์„œ MySql.Data ์„ค์น˜ํ•˜๊ธฐ

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ MySQL ์‚ฌ์šฉ์„ ์œ„ํ•œ ์ค€๋น„๋ฅผ ๋งˆ์นฉ๋‹ˆ๋‹ค.

//...
//MySQL namespace๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ ์–ธ.
using MySql.Data.MySqlClient;

namespace ExamMySQL
{
    public partial class Form1 : Form
    {
        string _server = "localhost"; //DB ์„œ๋ฒ„ ์ฃผ์†Œ, ๋กœ์ปฌ์ผ ๊ฒฝ์šฐ localhost
        int _port = 3308; //DB ์„œ๋ฒ„ ํฌํŠธ
        string _database = "new_schema"; //DB ์ด๋ฆ„
        string _id = "root"; //๊ณ„์ • ์•„์ด๋””
        string _pw = "root"; //๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ
        string _connectionAddress = "";

        public Form1()
        {
            InitializeComponent();
			//MySQL ์—ฐ๊ฒฐ์„ ์œ„ํ•œ ์ฃผ์†Œ ํ˜•์‹
            _connectionAddress = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4}", _server, _port, _database, _id, _pw);
        }
		
		//...
	}
}

WinForm UI ๊ตฌ์„ฑ

์ด๋ฆ„(name)๊ณผ ๋ฒˆํ˜ธ(phone) ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ, ์กฐํšŒํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹จ์ˆœํ•˜๊ฒŒ UI๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ ํ”„๋กœ์ ํŠธ UI ๊ตฌ์„ฑ

MySQL ๋ฐ์ดํ„ฐ ์‚ฝ์ž…(INSERT)

ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์‚ฝ์ž…์„ ์œ„ํ•œ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

INSERT INTO ํ…Œ์ด๋ธ”์ด๋ฆ„ [column1, column2, ...] VALUES (value1, value2, ...);

๊ทธ๋ฆฌ๊ณ  ์˜ˆ์ œ ์ฝ”๋“œ์˜ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

private void buttonInsert_Click(object sender, EventArgs e)
{
	try
	{
		using (MySqlConnection mysql = new MySqlConnection(_connectionAddress))
		{
			mysql.Open();
			//accounts_table์— name, phone column ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. id๋Š” ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
			string insertQuery = string.Format("INSERT INTO accounts_table (name, phone) VALUES ('{0}', '{1}');", textBoxName.Text, textBoxPhone.Text);

			MySqlCommand command = new MySqlCommand(insertQuery, mysql);
			if (command.ExecuteNonQuery() != 1)
				MessageBox.Show("Failed to insert data.");

			textBoxName.Text = "";
			textBoxPhone.Text = "";

			selectTable();
		}
	}
	catch (Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
}

MySQL ๋ฐ์ดํ„ฐ ์ˆ˜์ •(UPDATE)

ํ…Œ์ด๋ธ”์˜ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

UPDATE ํ…Œ์ด๋ธ”์ด๋ฆ„ SET column1 = value1 [, column2 = value2 ...] [WHERE ์กฐ๊ฑด];

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ˆ์ œ ์ฝ”๋“œ๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

private void buttonUpdate_Click(object sender, EventArgs e)
{
	try
	{
		using (MySqlConnection mysql = new MySqlConnection(_connectionAddress))
		{
			mysql.Open();
			int pos = listViewPhoneBook.SelectedItems[0].Index;
			int index = Convert.ToInt32(listViewPhoneBook.Items[pos].Text);
			//accounts_table์˜ ํŠน์ • id์˜ name column๊ณผ phone column ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
			string updateQuery = string.Format("UPDATE accounts_table SET name = '{1}', phone = '{2}' WHERE id={0};", index, textBoxName.Text, textBoxPhone.Text);

			MySqlCommand command = new MySqlCommand(updateQuery, mysql);
			if (command.ExecuteNonQuery() != 1)
				MessageBox.Show("Failed to delete data.");

			textBoxName.Text = "";
			textBoxPhone.Text = "";

			selectTable();
		}
	}
	catch (Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
}

MySQL ๋ฐ์ดํ„ฐ ์‚ญ์ œ(DELETE)

ํ…Œ์ด๋ธ”์˜ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

DELETE FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ [WHERE ์กฐ๊ฑด];

์˜ˆ์ œ ์ฝ”๋“œ๋„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

private void buttonDelete_Click(object sender, EventArgs e)
{
	try
	{
		using (MySqlConnection mysql = new MySqlConnection(_connectionAddress))
		{
			mysql.Open();
			int pos = listViewPhoneBook.SelectedItems[0].Index;
			int index = Convert.ToInt32(listViewPhoneBook.Items[pos].Text);
			//accounts_table์—์„œ ํŠน์ • id์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
			string deleteQuery = string.Format("DELETE FROM accounts_table WHERE id={0};", index);

			MySqlCommand command = new MySqlCommand(deleteQuery, mysql);
			if (command.ExecuteNonQuery() != 1)
				MessageBox.Show("Failed to delete data.");

			textBoxName.Text = "";
			textBoxPhone.Text = "";

			selectTable();
		}
	}
	catch (Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
}

MySQL ๋ฐ์ดํ„ฐ ์กฐํšŒ(SELECT)

ํ…Œ์ด๋ธ”์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

SELECT * FROM ํ…Œ์ด๋ธ”์ด๋ฆ„;

์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

private void selectTable()
{
	try
	{
		using (MySqlConnection mysql = new MySqlConnection(_connectionAddress))
		{
			mysql.Open();
			//accounts_table์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.            
			string selectQuery = string.Format("SELECT * FROM accounts_table");

			MySqlCommand command = new MySqlCommand(selectQuery, mysql);
			MySqlDataReader table = command.ExecuteReader();

			listViewPhoneBook.Items.Clear();

			while (table.Read())
			{
				ListViewItem item = new ListViewItem();
				item.Text = table["id"].ToString();
				item.SubItems.Add(table["name"].ToString());
				item.SubItems.Add(table["phone"].ToString());

				listViewPhoneBook.Items.Add(item);
			}

			table.Close();
		}
	}
	catch (Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
}

์ •๋ฆฌ

์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ๋”ฐ๋ผ ํ•˜์…จ๋‚˜์š”? DB ์—ฐ๊ฒฐ๋งŒ ์ œ๋Œ€๋กœ ํ•œ๋‹ค๋ฉด ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์˜ ์˜ˆ์ œ๋Š” ๋กœ์ปฌ ์„œ๋ฒ„ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋Š”๋ฐ, ์›๊ฒฉ ์„œ๋ฒ„์˜ DB๋„ ์ฃผ์†Œ๋งŒ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด ๋ฌธ์ œ์—†์ด ๋  ๊ฑฐ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ์•„๋ž˜ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ExamMySQL.zip
0.01MB

๋ฐ˜์‘ํ˜•