using System; using System.Collections.Generic; using MySql.Data.MySqlClient; // NuGet으로 설치한 MySQL.Data 패키지 참조 namespace MySQL_ConnTest { internal class Program { static void Main(string[] args) { // 연결 문자열(접속 정보) string connectionString = "Server=netlist.duckdns.org;" + "Port=3306;" + "Database=AutoManager;" + "Uid=AMmaster;" + "Pwd=AMmaster123!"; // 계정 정보를 저장할 리스트 (각 항목은 Dictionary) List> accountList = new List>(); try { // DB 연결 객체 생성 using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); // DB 연결 // account 테이블에서 id, passwd 컬럼 데이터 전부 조회 string query = "SELECT id, passwd FROM account"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) using (MySqlDataReader reader = cmd.ExecuteReader()) { // 결과를 한 행씩 읽으면서 accountList에 저장 while (reader.Read()) { var accountInfo = new Dictionary(); accountInfo["id"] = reader["id"].ToString(); accountInfo["passwd"] = reader["passwd"].ToString(); accountList.Add(accountInfo); } } } // 리스트에 저장된 데이터 출력 foreach (var accountInfo in accountList) { Console.WriteLine($"ID: {accountInfo["id"]}, PASSWD: {accountInfo["passwd"]}"); } Console.WriteLine("조회 완료"); } catch (Exception ex) { Console.WriteLine("오류 발생: " + ex.Message); } // 콘솔 창이 닫히지 않도록 대기 Console.WriteLine("아무 키나 누르면 종료됩니다."); Console.ReadKey(); } } }