Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- //tambahkan untuk oracle
- using System.Data.OracleClient;
- namespace Oracle_Dan_Datagridview
- {
- public partial class Form1 : Form
- {
- string connString;
- Boolean is_connecting;
- OracleConnection connection;
- public Form1()
- {
- InitializeComponent();
- this.loadingData();
- }
- private void makeConnection()
- {
- Console.WriteLine("\nBegin connecting...");
- connString = @"Data Source=xe;User ID=system;Password=123456;Unicode=True";
- connection = new OracleConnection(connString);
- //kalo menggunakan using block,
- //maka koneksi akan otomatis closed pada akhir using block,
- try
- {
- connection.Open();
- Console.WriteLine("Connected..");
- Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
- Console.WriteLine("State: {0}", connection.State);
- Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
- is_connecting = true;
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed to Connect::" + ex.Message);
- }
- }
- private void closeConnection()
- {
- if (is_connecting == true)
- {
- Console.WriteLine("\nClosing connection...");
- connection.Close();
- }
- }
- private void loadingData()
- {
- this.makeConnection();
- Console.WriteLine("\nBegin loading data...");
- try
- {
- OracleCommand command = new OracleCommand("SELECT * FROM PHONEBOOK", connection);
- OracleDataAdapter adapter = new OracleDataAdapter(command);
- DataTable table = new DataTable();
- adapter.Fill(table);
- dataGridViewPhonebook.DataSource = table;
- Console.WriteLine("Success loading data...");
- this.closeConnection();
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed to load data :: " + ex.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment