Selamat Sore pemirsa setia :D
kali ini saya akan bagikan source code c# untuk membuat koneksi dengan embedded database SQLite
Oke langsung saja
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.SQLite;
//.....//
public frm_department()
{
InitializeComponent();
}
//Tambahkan coding ini setelah constructor (constructor itu kira2 seperti diatas)
private SQLiteConnection conn;
private DataSet ds;
private SQLiteCommand cmd;
private SQLiteDataAdapter sda;
private SQLiteCommandBuilder cmdBuilder;
//Bagian Merah dibawah boleh diganti dengan nama database yang anda inginkan.. directorynya pun bebas terserah anda
private void exec(String query)
{
conn = new SQLiteConnection(@"Data Source=D:\School.sdb;
PRAGMA journal_mode = OFF;
PRAGMA synchronous = OFF;
PRAGMA locking_mode = EXCLUSIVE;
PRAGMA temp_store = MEMORY;
PRAGMA page_size = 4096;
PRAGMA cache_size = 10000;");
conn.Open();
ds = new DataSet();
cmd = conn.CreateCommand();
string sql = query;
sda = new SQLiteDataAdapter(sql, conn);
cmdBuilder = new SQLiteCommandBuilder(sda);
sda.Fill(ds);
}
Nah coding diatas untuk Procedure, sedangkan untuk mengisi Query seperti ini :
//Pembuatan Embedded database
exec("CREATE TABLE [Departments] ( " +
"[Department_ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"[Faculty_ID] INTEGER NOT NULL," +
"[Employee_ID] INTEGER NOT NULL," +
"[Title] TEXT NOT NULL," +
"[Description] TEXT NOT NULL," +
"[Address] TEXT NOT NULL," +
"[Phone] TEXT NOT NULL," +
"[Date_Register] datetime NOT NULL" +
");");
//Operasi Input Data
exec("insert into departments(faculty_id,employee_id, title, description, address, phone, date_register)"+
"values ("+
"'" + g_faculty_ID.SelectedItem + "',"+
"'" + g_Personnel_Id.SelectedItem + "'," +
"'" + txt_Title.Text + "'," +
"'" + txt_Description.Text + "'," +
"'" + txt_Address.Text + "'," +
"'" + txt_Phone.Text + "'," +
"datetime('now','localtime') );");
MessageBox.Show("Done!");
Jangan lupa di bagian Reference tambahkan SQLite.dll (seperti menambahkan reference Mysqlconnector pada Database server)
Oke sekian dan terimakasih
Bila ada yang ingin ditanyakan silahkan Comment disini :
Tidak ada komentar:
Posting Komentar