How to Connect MySQL Database in Asp.Net using C#

How to Connect MySQL Database in Asp.Net using C#

In this post we will discuss How to Connect MySQL Database in Asp.Net using C#, or you can say how to establish connection with MySQL server and display data in GridView control. To connect with MySQL you have to use MySQL connector for .net from here. Install connector on your system and copy paste .dll files from appropriate .net framework folder to application folder. After that you have to add MySQL namespace on your code page as mentioned below.

how to connect mysql database in using c#

In earlier post we have discussed about How to Install MySQL in Windows 7How To Add MySql As Linked Server In SQL ServerMySQL to SQL Server Data Migration Through SSIS and Create Database, Table in MySQL.

HTML Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>How to Connect MySQL Database in Asp.Net C#</title>
margin:0px auto;
<form id="form1" runat="server">
<asp:GridView ID="GridView1" Width="900px" runat="server"></asp:GridView>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    public void binddata()
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        MySqlConnection conn = new MySqlConnection(strcon);
        string strquery = "select * from student_info";
        MySqlCommand cmd = new MySqlCommand(strquery, conn);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        GridView1.DataSource = ds;


<?xml version="1.0"?>

  For more information on how to configure your ASP.NET application, please visit


      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    <add name="con" connectionString="Data Source=localhost;port=3306;Initial Catalog=student;User Id=root;password=root"/>