"C샵 MySQL 사용"의 두 판 사이의 차이

22번째 줄: 22번째 줄:


==Form1.cs 교체==
==Form1.cs 교체==
Form1.cs 의 내용을 다음으로 교체한다. (단, connStr는 자신의 환경에 맞게 수정해야 )
Form1.cs 의 내용을 다음으로 교체한다. (단, connStr는 자신의 환경에 맞게 수정해야 한다.<ref>이 소스는 '''[[윈도우 MySQL 설치]]''' 환경에서 테스트되었다.</ref>)
<source lang='csharp'>
<source lang='csharp'>
using System;
using System;
75번째 줄: 75번째 줄:
}
}
</source>
</source>
*실행


==주석==
<references/>


[[분류:Csharp]]
[[분류:Csharp]]
[[분류:MySQL]]
[[분류:MySQL]]

2013년 5월 15일 (수) 00:13 판

C# MySQL 커넥터 설치
C#에서 MySQL 사용하기

1 커넥터 다운로드

  • http://dev.mysql.com/downloads/connector/net/ 에 접속
  • Windows (x86, 32-bit), MSI Installer 오른쪽 [Download] 클릭
  • [No thanks, just take me to the downloads!] 클릭하여 mysql-connector-net-6.6.5.msi 다운로드 시작(12.7MB)

2 커넥터 설치

  • mysql-connector-net-6.6.5.msi 실행
  • "MySQL Connector Net 6.6.5 Setup" --- [Next]
  • [Typical] --- [Install] --- [Finish]

3 새 프로젝트

  • Visual Studio 실행
  • [파일(F)] --- [새로 만들기(N)] --- [프로젝트(P)...]
  • "새 프로젝트"[1] --- 이름(N): MySqlTest --- [확인]

4 커넥터 DLL 추가

  • "솔루션 탐색기" --- MySqlTest 우클릭 --- 참조 추가(R)...
  • "어셈블리 검색(Ctrl+E) 창에 mysql 입력 --- [v] MySql.Data --- [확인]

5 Form1.cs 교체

Form1.cs 의 내용을 다음으로 교체한다. (단, connStr는 자신의 환경에 맞게 수정해야 한다.[2])

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace MysqlTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<string> list = Query("show databases");
            MessageBox.Show(List2String(list));
        }

        public string List2String(List<string> list)
        {
            return string.Join("\n", list.Cast<string>().ToArray());
        }

        public List<string> Query(string queryStr)
        {
            string connStr = "server=localhost;user=root;password=P@ssw0rd";
            List<string> result = new List<string>();

            try
            {
                MySqlConnection conn = new MySqlConnection(connStr);
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(queryStr, conn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(dr.GetString(0));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }
    }
}
  • 실행

6 주석

  1. (기본값) .NET Framework 3.5, Windows Forms 응용 프로그램
  2. 이 소스는 윈도우 MySQL 설치 환경에서 테스트되었다.
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}