Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Collections;
- namespace LoadingDataToDataset
- {
- class Program
- {
- static string connString;
- static string sql;
- static DataSet ds;
- static void Main(string[] args)
- {
- Console.WriteLine("Mohon tunggu....");
- //bisa pakai berikut ini, tanpa simbol @,
- //string connString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=E:\\Temp\\LoadingDataToDataset\\LoadingDataToDataset\\Database1.mdf;Integrated Security=True";
- //bisa juga pakai berikut ini, pakai simbol @, simbol ini digunakan untuk mem-protect simbol slash \:
- connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\Temp\LoadingDataToDataset\LoadingDataToDataset\Database1.mdf;Integrated Security=True";
- sql = @"SELECT * FROM TabelSiswa";
- SqlConnection conn = new SqlConnection(connString);
- ds = new DataSet();
- try
- {
- conn.Open();
- SqlDataAdapter da = new SqlDataAdapter(sql, conn);
- da.Fill(ds);
- //
- Console.WriteLine("Transfer success");
- }
- catch (Exception e)
- {
- Console.WriteLine("Error: " + e);
- }
- finally
- {
- conn.Close();
- Console.WriteLine("Mari kita coba tambahkan DataTable....");
- //[BEGIN HERE...]
- DataTable table1 = new DataTable("NamaSekolah");
- table1.Columns.Add("Id");
- table1.Columns.Add("Sekolah");
- table1.Rows.Add(1, "SMANSAKRA");
- table1.Rows.Add(2, "SMANDAKRA");
- ds.Tables.Add(table1);
- Console.WriteLine("Penambahan berhasil, mari kita loading...");
- Console.WriteLine(ds.GetXml());
- }
- Console.WriteLine("\nKeren, loading dan tambah tabel berhasil, pencet tombol apa saja untuk mengeksekusi method berikutnya.");
- Console.ReadKey();
- selectData();
- Console.ReadLine();
- }
- private static void selectData()
- {
- //Sekarang, kita mencoba Method Based Query
- //untuk menyeleksi data dan menampilkannya
- DataTable sekolahku = ds.Tables["NamaSekolah"];
- var query = sekolahku.AsEnumerable().Select(NamaSekolah => new
- {
- IdSekolah = NamaSekolah.Field<string>("Id"),
- IdentitasSekolah = NamaSekolah.Field<string>("Sekolah")
- });
- //var query =
- // from NamaSekolah in sekolahku.AsEnumerable()
- // select NamaSekolah;
- foreach (var InformasiSekolah in query)
- {
- Console.WriteLine(InformasiSekolah.IdentitasSekolah);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement