C# and MySQL - How To Connect C# To MySQL And Display Data
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 :
2 - Connect And Display
1 comments:
commentsOne of The great blog on c#. go ahead
Reply