Membuat Aplikasi Database C# Menggunakan MS Access

Buat New Project, pilih Windows Form Application, lalu beri nama project.

db_1

Lalu buat tampilan GUI seperti berikut.

db_2

db_6

Lalu buat database menggunakan Microsoft Access

db_3

db_4

db_5

Setelah itu koneksikan C# dengan database MS Access yang sudah kita buat

db_8 db_9

Buatlah kodingan pada  C# sebagai berikut dengan  aktifkan beberapa event berikut.

  • ·         Form1_Load
  • ·         button1_Click
  • ·         button4_Click
  • ·         button3_Click
  • ·         button2_Click
  • ·         button5_Click

db_7

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 System.Data.OleDb;

namespace database2
{
    public partial class Form1 : Form
    {
        OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Documents and Settings\off\My Documents\Visual Studio 2010\Projects\database2\data_pass.accdb");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            vcon.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string vsql = string.Format("insert into user_pass values('{0}','{1}')", textBox1.Text, textBox2.Text);
            OleDbCommand vcom = new OleDbCommand(vsql, vcon);
            vcom.ExecuteNonQuery();
            MessageBox.Show("Data stored successfully");
            vcom.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string vsql = "select * from user_pass";
            OleDbCommand vcom = new OleDbCommand(vsql, vcon);
            DataSet vds = new DataSet();
            OleDbDataAdapter vda = new OleDbDataAdapter(vcom);
            vda.Fill(vds, "res");
            dataGridView1.DataSource = vds.Tables["res"];
            vda.Dispose();
            vcom.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string vsql = string.Format("select pass from user_pass where username = '{0}'", textBox3.Text);
            OleDbCommand vcom = new OleDbCommand(vsql, vcon);
            DataSet vds = new DataSet();
            OleDbDataAdapter vda = new OleDbDataAdapter(vcom);
            vda.Fill(vds,"res");
            dataGridView1.DataSource = vds.Tables["res"];
            try
            {
                label3.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception ex)
            {
                label3.Text = "kosong";
            }
            vda.Dispose();
            vcom.Dispose();
        }
    }
}

__________________

download sourcecode

______________________________

referensi: youtube.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s