-
[C# Winform] MySQL DB + Visual Studio 2022 ์ฐ๋C# 2022. 9. 14. 10:16
๐ก MySQL ์ค์น
MySQL :: Download Connector/NET
MySQL :: Download Connector/NET
MySQL Connector/NET 8.0 is highly recommended for use with MySQL Server 8.0, 5.7 and 5.6. Please upgrade to MySQL Connector/NET 8.0.
dev.mysql.com
๐ก ํ ์ด๋ธ ์์ฑ
์ค์ SQL ์์ฑํด๋ ๋๋ค. CREATE TABLE `test`.`db_test` ( `num` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, `age` INT NULL, `height` INT NULL, `weight` INT NULL, PRIMARY KEY (`num`));
๐ก INSERT INTO ... VALUES ์ด์ฉํด ๊ฐ ๋์
๐ก ํ๋ก์ ํธ ์์ฑ ํ MySql ์ค์น
: ๋๊ตฌ -> NuGet ํจํค์ง ๊ด๋ฆฌ์ -> ์๋ฃจ์ ์ฉ NuGet ํจํค์ง ๊ด๋ฆฌ...
๐ก Form1 ๋์์ธ : button + textBox
๐ก ์ฝ๋ ์์ฑ
using MySql.Data.MySqlClient; namespace WinFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // DB ์ฐ๊ฒฐ public static string cs = "server=localhost;userid=root;password=thein;database=test"; public static string query = "select * from db_test"; private void button1_Click(object sender, EventArgs e) { MySqlConnection myConn = new MySqlConnection(cs); // ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ MySqlCommand myQuery = new MySqlCommand(query, myConn); MySqlDataReader myReader; try { myConn.Open(); textBox1.AppendText("DB ์ ์ ์ฑ๊ณต : MySQL version =" + myConn.ServerVersion + "\r\n"); myReader = myQuery.ExecuteReader(); textBox1.AppendText("\r\n๋ฒํธ\t์ด๋ฆ\t๋์ด\tํค\t๋ชธ๋ฌด๊ฒ\r\n"); textBox1.AppendText("-----------------------------" + "\r\n"); while (myReader.Read()) { string num = myReader["num"].ToString(); string name = myReader["name"].ToString(); string age = myReader["age"].ToString(); string height = myReader["height"].ToString(); string weight = myReader["weight"].ToString(); textBox1.AppendText("\n" + num + "\t" + name + "\t" + age + "\t" + height + "\t" + weight + "\r\n"); } myConn.Close(); } catch (MySqlException ex) { textBox1.AppendText("DB ์ ์ ์คํจ : Error :" + ex.ToString()); } finally { if (myConn != null) { myConn.Close(); } } } } }
๐ก ์คํ
๊ฒฐ๊ณผ 'C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ๋ฐฐ์ด (0) 2022.09.14 [C#] break, continue, goto ๋ฐ๋ณต๋ฌธ ์ ์ด (0) 2022.09.13 [C#] for ๋ฌธ, while ๋ฌธ, do while ๋ฌธ, foreach ๋ฌธ (0) 2022.09.13 [C#] if๋ฌธ, switch (0) 2022.09.07 [C#] ๋ฐ์ดํฐ ํ์, ์ ์ถ๋ ฅ, ์ฐ์ฐ์ (0) 2022.09.06