Teknologi ODBC dapat digunakan
untuk membuat program database menggunakan hampir semua database asal mempunyai
driver ODBC yang telah terinstal di Windows seperti yang telah diterangkan
sebelumnya. Untuk membuat program database dengan Microsoft Access menggunakan
ODBC dengan kode program ikuti langkah-langkah berikut :
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.Odbc;
using
System.IO;
namespace AllDB
{
public partial class FAccessODBC : Form
{
public OdbcConnection cn;
public DataSet ds;
public DataTable tb;
public FAccessODBC()
{
InitializeComponent();
}
private void FAccessODBC_Load(object
sender, EventArgs e)
{
int i;
cn = new
OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};DBQ=" +
Path.GetDirectoryName(Application.ExecutablePath) + "\\world.mdb");
cn.Open();
OdbcCommand cm = new
OdbcCommand("SELECT name, address, phone FROM contacts", cn);
OdbcDataReader rd;
rd = cm.ExecuteReader();
ds = new DataSet();
tb = new DataTable();
tb.TableName = "contacts";
ds.Tables.Add(tb);
ds.Load(rd, LoadOption.Upsert, tb);
for (i = 0; i <
tb.Columns.Count; i++)
{
tb.Columns[i].MaxLength = -1;
}
rd.Close();
cn.Close();
bs.DataSource = ds;
bs.DataMember =
"contacts";
gContacts.DataSource = bs;
}
private void bs_CurrentChanged(object
sender, EventArgs e)
{
if ((bs.Count > 0) &&
(bs.DataMember == "contacts"))
{
tName.Text =
((DataRowView)bs[bs.Position])["name"].ToString();
tAddress.Text =
((DataRowView)bs[bs.Position])["address"].ToString();
tPhone.Text =
((DataRowView)bs[bs.Position])["phone"].ToString();
}
}
private void bFirst_Click(object
sender, EventArgs e)
{
bs.Position = 0;
}
private void bPrev_Click(object sender,
EventArgs e)
{
bs.Position = bs.Position - 1;
}
private void bNext_Click(object sender,
EventArgs e)
{
bs.Position = bs.Position + 1;
}
private void bLast_Click(object sender,
EventArgs e)
{
bs.Position = bs.Count - 1;
}
private void bAdd_Click(object sender,
EventArgs e)
{
bs.AddNew();
}
private void bEdit_Click(object sender,
EventArgs e)
{
//
}
private void bSave_Click(object sender,
EventArgs e)
{
OdbcCommand cm = new OdbcCommand();
cn.Open();
if
(((DataRowView)bs[bs.Position])["name"].ToString() == "")
{
cm.Dispose();
cm = new
OdbcCommand("INSERT INTO contacts (name, address, phone) VALUES (?, ?,
?)", cn);
}
else
{
cm.Dispose();
cm = new
OdbcCommand("UPDATE contacts SET name = ?, address = ?, phone = ? WHERE name
= ?", cn);
}
cm.Parameters.Add("@name", OdbcType.VarChar, 0,
"").Value = tName.Text;
cm.Parameters.Add("@address", OdbcType.VarChar, 0,
"").Value = tAddress.Text;
cm.Parameters.Add("@phone", OdbcType.VarChar, 0,
"").Value = tPhone.Text;
if
(((DataRowView)bs[bs.Position])["name"].ToString() != "")
{
cm.Parameters.Add("@Original_name", OdbcType.VarChar, 0,
"").Value =
((DataRowView)bs[bs.Position])["name"].ToString();
}
cm.ExecuteNonQuery();
cn.Close();
((DataRowView)bs[bs.Position])["name"] = tName.Text;
((DataRowView)bs[bs.Position])["address"] = tAddress.Text;
((DataRowView)bs[bs.Position])["phone"]
= tPhone.Text;
bs.EndEdit();
}
private void bCancel_Click(object
sender, EventArgs e)
{
bs.CancelEdit();
}
private void bDelete_Click(object
sender, EventArgs e)
{
cn.Open();
OdbcCommand cm = new
OdbcCommand("DELETE FROM contacts WHERE name = ?", cn);
cm.Parameters.Add("@Original_name", OdbcType.VarChar, 0,
"").Value =
((DataRowView)bs[bs.Position])["name"].ToString();
cm.ExecuteNonQuery();
cn.Close();
bs.RemoveAt(bs.Position);
}
private void bRefresh_Click(object
sender, EventArgs e)
{
int i;
gContacts.DataSource = null;
bs.DataSource = null;
ds.Dispose();
tb.Dispose();
cn.Open();
OdbcCommand cm = new
OdbcCommand("SELECT name, address, phone FROM contacts WHERE name LIKE
'%" + tFind.Text.Trim() + "%'", cn);
OdbcDataReader rd;
rd = cm.ExecuteReader();
ds = new DataSet();
tb = new DataTable();
tb.TableName =
"contacts";
ds.Tables.Add(tb);
ds.Load(rd, LoadOption.Upsert, tb);
for (i = 0; i < tb.Columns.Count;
i++)
{
tb.Columns[i].MaxLength = -1;
}
rd.Close();
cn.Close();
bs.DataSource = ds;
bs.DataMember =
"contacts";
gContacts.DataSource = bs;
}
}
}
Setelah program dijalankan hasilnya tampak seperti tampilan
berikut :
Membuat aplikasi database menggunakan MySQL dapat dilakukan
memakai Driver ODBC (System.Data.Odbc), dengan syarat terlebih dahulu telah
diinstall Driver MyODBC untuk MySQL yang dapat didownload gratis di internet.
Membuat aplikasi database MySQL menggunakan ODBC (System.Data.Odbc) cara
pemrogramannya mirip dengan Odbc saat membuat aplikasi database menggunakan
Access, hanya saja ConnectionString-nya diganti sebagai berikut :
cn = new OdbcConnection("DRIVER={MySQL
ODBC 3.51
Driver};UID=root;STMT=;OPTION=;PORT=;PASSWORD=;SERVER=localhost;DATABASE=world;DESC=");
ConnectionString tersebut dapat dilihat susunannya dari
Registry.
Membuat aplikasi database menggunakan Interbase atau Firebird
dapat dilakukan memakai Driver ODBC (System.Data.Odbc), dengan syarat terlebih
dahulu telah diinstall Driver Firebird/Interbase ODBC untuk Interbase/Firebird
yang dapat didownload gratis di internet. Membuat aplikasi database
Firebird/Interbase menggunakan ODBC (System.Data.Odbc) cara pemrogramannya
mirip dengan Odbc saat membuat aplikasi database menggunakan Access maupun
MySQL, hanya saja ConnectionString-nya diganti sebagai berikut :
cn = new OdbcConnection("DRIVER={Firebird/InterBase(r)
driver};Dbname=" + Path.GetDirectoryName(Application.ExecutablePath) +
"\\WORLD.FDB;User=SYSDBA;PASSWORD=DKEBFJENHFCOBHGHLAIMNAAFICELEAEGDNMFNOGALAMHBBGCHFADNKCBPPGMANOGIEKENIOPHDIPBIECPLLLCBIKEJKMJLPLIB");