C# and MySQL - How To Connect C# To MySQL And Display Data

C# and MySQL - How To Connect C# To MySQL And Display Data

                                                                                                                                         

In This C# Tutorials We Will See How To Connect CSharp  To MySQL DataBase .

                                              
                                                        1 - Connect To MySQL 

2 - Connect And Show Data In Datagridview

Project Source Code:

1 - Connect

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

namespace Csharp_And_MySQL
{
    public partial class
Csharp_Connect_To_MySQL_Database : Form
    {
        public
Csharp_Connect_To_MySQL_Database()
        {
            InitializeComponent();
        }
        MySqlConnection connection;
        private void
Csharp_Connect_To_MySQL_Database_Load(object sender, EventArgs e)
        {
           
try
            {
                connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
                connection.Open();
                if(connection.State == ConnectionState.Open)
                {
                    label1.Text = "Connected";
                    label1.ForeColor = Color.Green;
                }
                else
                {
                    label1.Text = "Not Connected";
                    label1.ForeColor = Color.Red;
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
                label1.Text = "Not Connected";
                label1.ForeColor = Color.Red;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
                label1.Text = "Connected";
                label1.ForeColor = Color.Green;
            }
        }
    }

}


=> OUTPUT :

How To Connect C# To MySQL Database


2 - Connect And Display

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace
Csharp_And_MySQL
{
    public partial class Csharp_Display_Data_From_Mysql_Into_Datagridview : Form
    {
        public Csharp_Display_Data_From_Mysql_Into_Datagridview()
        {
            InitializeComponent();
        }
        private void Csharp_Display_Data_From_Mysql_Into_Datagridview_Load(object sender, EventArgs e)
        {
            
            try
            {
                MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
                MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM test_db.users", connection);
                connection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds, "users");
                dataGridView1.DataSource = ds.Tables["users"];
                connection.Close();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }

}


=> OUTPUT :

populate datagridview from mysql database in c#




Share this

Related Posts

Previous
Next Post »

1 comments:

comments
7 février 2017 à 05:18 delete

One of The great blog on c#. go ahead

Reply
avatar