Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // buat objek item beli (3 objek)
            var item1 = new ItemBeli
            {
                BarangID = "BB-7421",
                Jumlah = 5,
                HargaBeli = 54,
                HargaJual = 60
            };

            var item2 = new ItemBeli
            {
                BarangID = "BB-8107",
                Jumlah = 7,
                HargaBeli = 101,
                HargaJual = 110
            };

            var item3 = new ItemBeli
            {
                BarangID = "BK-M18B-44",
                Jumlah = 10,
                HargaBeli = 540,
                HargaJual = 693
            };

            // buat objek beli
            var beli = new Beli
            {
                Nota = "N001",
                SupplierID = 3,
                Tanggal = DateTime.Today,
                Keterangan = "Transaksi beli menggunakan Entity Framework"
            };

            // tambahkan item beli ke objek beli
            beli.ItemBelis.Add(item1);
            beli.ItemBelis.Add(item2);
            beli.ItemBelis.Add(item3);

            var result = AddPembelianUsingEF(beli);

            Console.WriteLine("\nStatus transaksi : {0}", result == 1 ? "Sukses" : "Gagal");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        private static IList<ItemBeli> GetDetailPembelian(string nota, SqlConnection conn)
        {
            var daftarItemBeli = new List<ItemBeli>();

            var sql = @"SELECT Barang.BarangID, Barang.NamaBarang, ItemBeli.Jumlah, ItemBeli.HargaBeli, ItemBeli.HargaJual
                        FROM Barang INNER JOIN ItemBeli ON Barang.BarangID = ItemBeli.BarangID
                        WHERE ItemBeli.Nota = @1";
            using (var cmd = new SqlCommand(sql, conn))
            {
                cmd.Parameters.AddWithValue("@1", nota);

                using (var dtr = cmd.ExecuteReader())
                {
                    while (dtr.Read())
                    {
                        // mapping record ke objek barang
                        var barang = new Barang
                        {
                            BarangID = dtr["BarangID"] is DBNull ? string.Empty : (string)dtr["BarangID"],
                            NamaBarang = dtr["NamaBarang"] is DBNull ? string.Empty : (string)dtr["NamaBarang"]
                        };

                        // mapping record ke objek item beli
                        var itemBeli = new ItemBeli
                        {
                            Barang = barang, // hubungkan barang dg item beli
                            Jumlah = dtr["Jumlah"] is DBNull ? 0 : (int)dtr["Jumlah"],
                            HargaBeli = dtr["HargaBeli"] is DBNull ? 0 : (int)dtr["HargaBeli"],
                            HargaJual = dtr["HargaJual"] is DBNull ? 0 : (int)dtr["HargaJual"]
                        };

                        daftarItemBeli.Add(itemBeli);
                    }
                }
            }

            return daftarItemBeli;
        }