본문 바로가기
Programming

.net과 mysql 연동하기

by Mizix 2010. 6. 7.
반응형
1. MySqlConnecter 설치
2. 참조 -> 참조 추가 -> MySql.Data.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//DB접속 함수.
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;


namespace studyADONET
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 데이터베이스 위치 정보 및 계정 정보를 저장하는 연결 문자열
            string strCon = "server=localhost;database=intra;uid=intra;pwd=intra";

            // 데이터베이스와 연결을 담당하는 객체 생성
            // 연결 문자열을 파라미터로 전달
            MySqlConnection connection;
            connection = new MySqlConnection();

            connection.ConnectionString = strCon;
            try
            {
                Response.Write("데이터베이스 연결중...");
                connection.Open();
                Response.Write("<br>데이터베이스 연결 성공");

                Response.Write("<br>현재 데이터베이스 : " + connection.Database);
                Response.Write("<br>연결 상태 : " + connection.State);
                Response.Write("<br>데이터베이스 버전 : " + connection.ServerVersion);
            }
            catch (Exception E)
            {
                Response.Write("데이터베이스 연결 실패");
            }
            connection.Close();
            Response.Write("<br>연결 상태 : " + connection.State);
        }
    }
}
반응형