OKIEWARDOYO

VISUAL C# No.39 full

Feb 17th, 2014
4,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 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. using System.Data.SqlClient;//tambahkan directive ini untuk sql
  12. using System.IO; //digunakan untuk path
  13. using System.Reflection; //digunakan untuk assembly
  14.  
  15. namespace DataGridView_SqlServer
  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.         SqlConnection connection;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.  
  32.             this.LoadingData();
  33.         }
  34.  
  35.         private void makeConnection(){
  36.             Console.WriteLine("\nBegin connecting...");
  37.  
  38.             mdfFilename = "Database1.mdf";
  39.             outputFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  40.             attachDbFilename = Path.Combine(outputFolder, mdfFilename);
  41.  
  42.             connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + attachDbFilename + ";Integrated Security=True";
  43.  
  44.             connection = new SqlConnection(connString);
  45.  
  46.             //kalo menggunakan using block,
  47.             //maka koneksi akan otomatis closed pada akhir using block,
  48.  
  49.             try
  50.             {
  51.  
  52.                 connection.Open();
  53.  
  54.                 Console.WriteLine("Connected..");
  55.                 Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
  56.                 Console.WriteLine("State: {0}", connection.State);
  57.                 Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
  58.  
  59.                 is_connecting = true;
  60.                
  61.             }
  62.             catch (Exception ex)
  63.             {
  64.                 Console.WriteLine("Failed to Connect::" + ex.Message);
  65.                
  66.             }
  67.         }
  68.  
  69.         private void closeConnection(){
  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.                 SqlCommand command = new SqlCommand("SELECT * FROM TablePerson", connection);
  87.                 SqlDataAdapter adapter = new SqlDataAdapter(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. }
Advertisement
Add Comment
Please, Sign In to add comment