OKIEWARDOYO

VISUAL C# No.47 2

Aug 12th, 2014
1,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. namespace LoadingDataToDataset
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Mohon tunggu....");
  12.  
  13.             //bisa pakai berikut ini, tanpa simbol @,
  14.             //string connString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=E:\\Temp\\LoadingDataToDataset\\LoadingDataToDataset\\Database1.mdf;Integrated Security=True";
  15.             //bisa juga pakai berikut ini, pakai simbol @, simbol ini digunakan untuk mem-protect simbol slash \:
  16.  
  17.             string connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\Temp\LoadingDataToDataset\LoadingDataToDataset\Database1.mdf;Integrated Security=True";
  18.             string sql = @"SELECT * FROM TabelSiswa";
  19.             SqlConnection conn = new SqlConnection(connString);
  20.             DataSet ds = new DataSet();
  21.  
  22.             try
  23.             {
  24.                 conn.Open();
  25.                 SqlDataAdapter da = new SqlDataAdapter(sql, conn);
  26.                 da.Fill(ds);
  27.                 //
  28.                 Console.WriteLine("Transfer success");
  29.             }
  30.             catch (Exception e)
  31.             {
  32.                 Console.WriteLine("Error: " + e);
  33.             }
  34.             finally
  35.             {
  36.                 conn.Close();
  37.                 Console.WriteLine("Mari kita coba tambahkan DataTable....");
  38.  
  39.                 //[BEGIN HERE...]
  40.                 DataTable table1 = new DataTable("NamaSekolah");
  41.                 table1.Columns.Add("Id");
  42.                 table1.Columns.Add("Sekolah");
  43.                 table1.Rows.Add(1, "SMANSAKRA");
  44.                 table1.Rows.Add(2, "SMANDAKRA");
  45.                 ds.Tables.Add(table1);
  46.  
  47.                 Console.WriteLine("Penambahan berhasil, mari kita loading...");
  48.                 Console.WriteLine(ds.GetXml());
  49.             }
  50.  
  51.             Console.ReadLine();
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment