Pertama Persiapkan Visual Studio Di pC anda
install itu di PC anda
dan create new project dan pilih windowsform
dan create new project dan pilih windowsform
lalu pilih label untuk (kartunama,nama,alamat,no tlpn,email,namaperusahaan)
pilih juga textBox untuk (insert data)
pilih button untuk (save,clear,update)
setelah itu paste kan script di bawah ini
========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace connect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
masukTabel();
}
private void inputDatabase()
{
try
{ //Koneksi string untuk menunjuk ke alamat database
string konek = "server=localhost; database=visual1; username=root; password=;";
//Melakukan insert ke database melalui form
string Query = "insert into gilangvisual(nama,alamat,no_tlpn,email,nama_perusahaan) values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "');";
//Membuat object koneksi
MySqlConnection knk = new MySqlConnection(konek);
//perintah untuk melakukan koneksi
MySqlCommand perintah = new MySqlCommand(Query, knk);
MySqlDataReader bacaData;
knk.Open();
bacaData = perintah.ExecuteReader();
//Disini untuk query agar tersimpan dalam database
MessageBox.Show("Data Berhasil Disimpan");
while (bacaData.Read())
{
}
knk.Close();
}
catch (Exception)
{
MessageBox.Show("Data gagal disimpan");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
{
inputDatabase();
masukTabel();
}
else
{
MessageBox.Show("Data belum lengkap","");
}
}
private void masukTabel()
{
try
{
string konek = "server=localhost; database=visual1; username=root; password=;";
MySqlConnection knk = new MySqlConnection(konek);
MySqlCommand perintah = new MySqlCommand("select nama as 'nama',alamat as 'alamat',
no_tlpn as 'no_tlpn',email as 'email',nama_perusahaan as 'nama_perusahaan'from gilangvisual;", knk);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = perintah;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
string connect = "server=localhost; database=visual1; username=root; password=;";
string Query = "delete from gilangvisual where nama='" + this.textBox1.Text + "';";
MySqlConnection knk = new MySqlConnection(connect);
MySqlCommand perintah = new MySqlCommand(Query, knk);
MySqlDataReader bacaData;
try
{
knk.Open();
bacaData = perintah.ExecuteReader();
MessageBox.Show("Data Berhasil Di Hapus");
while (bacaData.Read())
{
}
knk.Close();
masukTabel();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)
{
string connect = "server=localhost; database=visual1; username=root; password=;";
string Query = "update gilangvisual set nama='" + this.textBox1.Text + "',alamat='" + this.textBox2.Text + "',no_tlpn='" + this.textBox3.Text + "',email='" + this.textBox4.Text + "',nama_perusahaan='" + this.textBox5.Text + "';";
MySqlConnection knk = new MySqlConnection(connect);
MySqlCommand perintah = new MySqlCommand(Query, knk);
MySqlDataReader bacaData;
try
{
knk.Open();
bacaData = perintah.ExecuteReader();
MessageBox.Show("Data Berhasil Di Update");
while (bacaData.Read())
{
}
knk.Close();
masukTabel();
}
catch (Exception)
{
MessageBox.Show("Data Gagal Disimpan");
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
textBox1.Text = row.Cells["nama"].Value.ToString();
textBox2.Text = row.Cells["alamat"].Value.ToString();
textBox3.Text = row.Cells["no_tlpn"].Value.ToString();
textBox4.Text = row.Cells["email"].Value.ToString();
textBox5.Text = row.Cells["nama_perusahaan"].Value.ToString();
}
}
}
}
========================================================================
Setelah selesai pilih debug progam
dan akan muncul seperti dibawah ini
SELAMAT MENCOBA
dan akan muncul seperti dibawah ini
SELAMAT MENCOBA
Comments
Post a Comment
terima kasih telah berpartisipasi pada blog kami