Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

DATABASE CONNECTION C# (MYSQL)

Database Name : sampledatabase

Table Name: infotbl

Fields: id int

name varchar(255)

address varchar(50)

Open Visual Studio > New Project > Solution Explorer(F4) > right click on “REFERENCE” > all MySql.dll(win apps), MySql.web.dll(web
apps))

Add to header:

using MySql.Data.MySqlClient;

txtid txtname

DataGridView1

txtMobile

SOURCE CODE

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

namespace WindowsFormsApplication6
{
publicpartialclassForm1 : Form
{
String MyConnectionString =
"Server=localhost;Database=sampledatabase;Uid=root;Pwd="; public Form1()
{
InitializeComponent();
}

privatevoid Form1_Load(object sender, EventArgs e)


{

LoadData();

Private void button1_Click(object sender, EventArgs e)//SAVE BUTTON


{
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
try

1
Prepared by: Rochelle B. Cadayong
{
cmd = connection.CreateCommand();
cmd.CommandText = "Insert into infotbl (id, name, address)values(@id, @name,@address)";
cmd.Parameters.AddWithValue("@id", int.Parse(txtid.Text));
cmd.Parameters.AddWithValue("@name", (txtname.Text));
cmd.Parameters.AddWithValue("@address", (txtaddress.Text));
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Added!");
}
catch (Exception)
{
throw;

}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
LoadData();
}
} }

Private void LoadData()//FUNCTION


{
MySqlConnection connection = new MySqlConnection(MyConnectionString);
connection.Open();
try
{
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "Select * From infotbl ";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
DataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch (Exception)
{
throw;
}
}

Private void button2_Click(object sender, EventArgs e)//DELETE BUTTON


{
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd2;
connection.Open();
try
{
cmd2 = connection.CreateCommand();
cmd2.CommandText = "Delete from infotbl where id = @id";
cmd2.Parameters.AddWithValue("@id", int.Parse(txtid.Text));
cmd2.ExecuteNonQuery();
MessageBox.Show("Successfully Deleted!");
txtid.Clear();
txtname.Clear();
txtaddress.Clear();
txtid.Focus();
}
catch (Exception)
{
throw;

}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
LoadData();
}
}
2
Prepared by: Rochelle B. Cadayong
}

Private void button3_Click(object sender, EventArgs e)//SEARCH


BUTTON {
MySqlConnection connection = new
MySqlConnection(MyConnectionString); connection.Open();
try
{
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "Select * From infotbl where id = @id";
cmd.Parameters.AddWithValue("@id", int.Parse(txtid.Text));
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);

DataSet ds = new DataSet();


adap.Fill(ds);
dgd.DataSource = ds.Tables[0].DefaultView;
txtid.Text = Convert.ToString(ds.Tables[0].Rows[0][0]); txtname.Text =
Convert.ToString(ds.Tables[0].Rows[0][1]); txtaddress.Text =
Convert.ToString(ds.Tables[0].Rows[0][2]);

}
catch (Exception)
{
throw;

} }

Private void button4_Click(object sender, EventArgs e)//UPDATE


BUTTON {
MySqlConnection connection = new
MySqlConnection(MyConnectionString); MySqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "Update infotbl SET name=@name, address=@address where
id=@id"; cmd.Parameters.AddWithValue("@id", int.Parse(txtid.Text));
cmd.Parameters.AddWithValue("@name", (txtname.Text));
cmd.Parameters.AddWithValue("@address", (txtaddress.Text));
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Updated!");
txtid.Clear();
txtname.Clear();
txtaddress.Clear();
txtid.Focus();
}
catch (Exception)
{
throw;

}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
LoadData();
}
}}
3
Prepared by: Rochelle B. Cadayong

You might also like