OKIEWARDOYO

C# No.40: full

Mar 26th, 2014
1,640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  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. //tambahkan untuk oracle
  12. using System.Data.OracleClient;
  13.  
  14. namespace Oracle_Dan_Datagridview
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         string connString;
  19.         Boolean is_connecting;
  20.  
  21.         OracleConnection connection;
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.             this.loadingData();
  27.         }
  28.  
  29.         private void makeConnection()
  30.         {
  31.             Console.WriteLine("\nBegin connecting...");
  32.  
  33.             connString = @"Data Source=xe;User ID=system;Password=123456;Unicode=True";
  34.  
  35.             connection = new OracleConnection(connString);
  36.  
  37.             //kalo menggunakan using block,
  38.             //maka koneksi akan otomatis closed pada akhir using block,
  39.  
  40.             try
  41.             {
  42.  
  43.                 connection.Open();
  44.  
  45.                 Console.WriteLine("Connected..");
  46.                 Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
  47.                 Console.WriteLine("State: {0}", connection.State);
  48.                 Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
  49.  
  50.                 is_connecting = true;
  51.  
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 Console.WriteLine("Failed to Connect::" + ex.Message);
  56.  
  57.             }
  58.         }
  59.  
  60.         private void closeConnection()
  61.         {
  62.             if (is_connecting == true)
  63.             {
  64.                 Console.WriteLine("\nClosing connection...");
  65.                 connection.Close();
  66.             }
  67.  
  68.         }
  69.  
  70.         private void loadingData()
  71.         {
  72.             this.makeConnection();
  73.  
  74.             Console.WriteLine("\nBegin loading data...");
  75.  
  76.             try
  77.             {
  78.                 OracleCommand command = new OracleCommand("SELECT * FROM PHONEBOOK", connection);
  79.                 OracleDataAdapter adapter = new OracleDataAdapter(command);
  80.                 DataTable table = new DataTable();
  81.  
  82.                 adapter.Fill(table);
  83.  
  84.                 dataGridViewPhonebook.DataSource = table;
  85.  
  86.                 Console.WriteLine("Success loading data...");
  87.  
  88.                 this.closeConnection();
  89.  
  90.             }
  91.             catch (Exception ex)
  92.             {
  93.                 Console.WriteLine("Failed to load data :: " + ex.Message);
  94.             }
  95.         }
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment