document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using System.Data.OleDb;//tambahkan directive ini untuk OleDb
  12. using System.IO; //digunakan untuk path
  13. using System.Reflection; //digunakan untuk assembly
  14.  
  15. namespace DataGridView_OleDb_MsAccess
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         string mdfFilename;
  20.         string outputFolder;
  21.         string attachDbFilename;
  22.  
  23.         string connString;
  24.         Boolean is_connecting;
  25.  
  26.         OleDbConnection connection;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.             this.LoadingData();
  32.         }
  33.  
  34.         private void makeConnection()
  35.         {
  36.             Console.WriteLine("\\nBegin connecting...");
  37.  
  38.             mdfFilename = "Database1.accdb";
  39.             outputFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  40.             attachDbFilename = Path.Combine(outputFolder, mdfFilename);
  41.  
  42.             connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + attachDbFilename;
  43.             connection = new OleDbConnection(connString);
  44.  
  45.             //kalo menggunakan using block,
  46.             //maka koneksi akan otomatis closed pada akhir using block,
  47.  
  48.             try
  49.             {
  50.  
  51.                 connection.Open();
  52.  
  53.                 Console.WriteLine("Connected..");
  54.                 Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
  55.                 Console.WriteLine("State: {0}", connection.State);
  56.                 Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
  57.  
  58.                 is_connecting = true;
  59.  
  60.             }
  61.             catch (Exception ex)
  62.             {
  63.                 Console.WriteLine("Failed to Connect::" + ex.Message);
  64.  
  65.             }
  66.         }
  67.  
  68.         private void closeConnection()
  69.         {
  70.             if (is_connecting == true)
  71.             {
  72.                 Console.WriteLine("\\nClosing connection...");
  73.                 connection.Close();
  74.             }
  75.  
  76.         }
  77.  
  78.         private void LoadingData()
  79.         {
  80.             this.makeConnection();
  81.  
  82.             Console.WriteLine("\\nBegin loading data...");
  83.  
  84.             try
  85.             {
  86.                 OleDbCommand command = new OleDbCommand("SELECT * FROM TablePerson", connection);
  87.                 OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  88.                 DataTable table = new DataTable();
  89.  
  90.                 adapter.Fill(table);
  91.  
  92.                 dataGridView1.DataSource = table;
  93.  
  94.                 Console.WriteLine("Success loading data...");
  95.  
  96.                 this.closeConnection();
  97.  
  98.             }
  99.             catch (Exception ex)
  100.             {
  101.                 Console.WriteLine("Failed to load data :: " + ex.Message);
  102.             }
  103.         }
  104.     }
  105. }
');