Contoh Koneksi C# dengan MS Access 2003 (*.mdb) dan 2007 (*.accdb)

Artikel ini adalah lanjutan/contoh program dari artikel Pengetahuan Dasar  ADO.NET   C#  untuk Pemrograman Database


using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text; 

/*
untuk Ms aceess2007 ganti connStr dengan:
@"Provider=Microsoft.ACE.OLEDB.12.0;  Data Source=D:\dataaccess2007.accdb;Persist Security Info=False;"
*/  

private void cmdConnect_Click(object sender, EventArgs e)
        {
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\dataaccess2003.mdb;Persist Security Info=False;";
        if (connMsAccess != null)
            connMsAccess.Close();

            string connStrMsaccess = String.Format(connStr);

            try
            {
                connMsAccess = new OleDbConnection(connStrMsAccess);
                connMsAccess.Open();

                txtRemark.Text = "ONLINE";
                 tmrUpdate_datanya.Enabled = true;

            }
            catch
            {
                this.txtRemark.Text = "koneksi gagal";

            }
        }

private void tmrupdate_datanya_Tick(object sender, EventArgs e)
        {
            OleDbCommand cmdMsaccess = new OleDbCommand();
            cmdMsaccess.Connection = connMsAccess;
            cmdMsaccess.CommandText = "select * from tabel_output_akhir where id = 1";

            OleDbDataReader ReaderMsAccess = null;
            try
            {

                ReaderMsAccess = cmdMsaccess.ExecuteReader();

                while (ReaderMsAccess.Read())
                txtPlanF.Text = reader["plan"].ToString();
                txtTargetF.Text = reader["target"].ToString();
                txtActualF.Text = reader["actual"].ToString();

                ReaderMsAccess.Close();
                txtRemarkG.Text = "Network ok";
            }

            catch
            {
                txtRemarkG.Text = "terputus";
            }
        }
Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis untuk pemula.

Posted on 05/08/2011, in Dasar Basisdata (Database), Visual C#. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

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

%d bloggers like this: