001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Drawing;
006.
using
System.Linq;
007.
using
System.Text;
008.
using
System.Threading.Tasks;
009.
using
System.Windows.Forms;
010.
using
MySql.Data.MySqlClient;
011.
012.
namespace
WindowsFormsApplication1
013.
{
014.
public
partial
class
frmHome : Form
015.
{
016.
public
frmHome()
017.
{
018.
InitializeComponent();
019.
}
020.
021.
private
void
frmHome_Load(
object
sender, EventArgs e)
022.
{
023.
BindDataGrid();
024.
}
025.
026.
private
void
BindDataGrid()
027.
{
028.
DataTable dt =
new
DataTable();
029.
string
myConnection =
"datasource=localhost;port=3306;username=root;password=1234;database=aprdb;"
;
030.
MySqlConnection myConn =
new
MySqlConnection(myConnection);
031.
MySqlDataAdapter myDataAdapter =
new
MySqlDataAdapter();
032.
myConn.Open();
033.
034.
035.
036.
MySqlCommand myCommand = myConn.CreateCommand();
037.
myCommand.CommandText =
"SELECT * FROM table;"
;
038.
myCommand.CommandType = CommandType.Text;
039.
040.
myDataAdapter =
new
MySqlDataAdapter(myCommand);
041.
myDataAdapter.Fill(dt);
042.
043.
044.
045.
myConn.Close();
046.
047.
048.
this
.dgName.DataSource = dt;
049.
050.
this
.dgName.Columns.Clear();
051.
052.
DataGridViewTextBoxColumn column;
053.
054.
column =
new
DataGridViewTextBoxColumn();
055.
column.DataPropertyName =
"Username"
;
056.
column.HeaderText =
"Username"
;
057.
column.Width = 50;
058.
this
.dgName.Columns.Add(column);
059.
060.
column =
new
DataGridViewTextBoxColumn();
061.
column.DataPropertyName =
"Name"
;
062.
column.HeaderText =
"Name"
;
063.
column.Width = 100;
064.
this
.dgName.Columns.Add(column);
065.
066.
column =
new
DataGridViewTextBoxColumn();
067.
column.DataPropertyName =
"Email"
;
068.
column.HeaderText =
"Email"
;
069.
column.Width = 150;
070.
this
.dgName.Columns.Add(column);
071.
072.
dt =
null
;
073.
}
074.
075.
private
void
btn_add_Click(
object
sender, EventArgs e)
076.
{
077.
078.
}
079.
080.
private
void
btn_edit_Click(
object
sender, EventArgs e)
081.
{
082.
083.
}
084.
085.
private
void
btn_del_Click(
object
sender, EventArgs e)
086.
{
087.
088.
}
089.
090.
private
void
btn_exit_Click(
object
sender, EventArgs e)
091.
{
092.
if
(MessageBox.Show(
"Are you sure to exit program?"
,
"Confirm."
, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
093.
{
094.
Application.Exit();
095.
}
096.
}
097.
098.
private
void
btn_check_Click(
object
sender, EventArgs e)
099.
{
100.
try
101.
{
102.
string
myConnection =
"datasource=localhost;port=3306;username=root;password=1234;database=aprdb;"
;
103.
MySqlConnection myConn =
new
MySqlConnection(myConnection);
104.
MySqlDataAdapter myDataAdapter =
new
MySqlDataAdapter();
105.
myDataAdapter.SelectCommand =
new
MySqlCommand(
"select * from table;"
,myConn);
106.
MySqlCommandBuilder cb =
new
MySqlCommandBuilder(myDataAdapter);
107.
myConn.Open();
108.
DataSet ds =
new
DataSet();
109.
MessageBox.Show(
"Connected"
);
110.
myConn.Close();
111.
112.
}
113.
catch
(Exception ex)
114.
{
115.
MessageBox.Show(ex.Message);
116.
}
117.
}
118.
}
119.
}