Exemplo n.º 1
0
        public int UpdateStok(Barang barang)
        {
            int            result = 0;
            SqlTransaction trans  = null;

            try
            {
                trans = _conn.BeginTransaction();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection  = _conn;
                    cmd.Transaction = trans;
                    cmd.CommandText = @"update barang set 
                        stok = @stok where kode = @kode";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@kode", barang.Kode);
                    cmd.Parameters.AddWithValue("@stok", barang.Stok);
                    result = cmd.ExecuteNonQuery();
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                throw ex;
            }
            finally
            {
                if (trans != null)
                {
                    trans.Dispose();
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public Barang GetDataBarangByKode(string kode)
        {
            Barang result = null;

            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection  = _conn;
                    cmd.CommandText = @"select * from barang Where kode = @Kode";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@Kode", kode);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                result = new Barang
                                {
                                    Kode   = reader["Kode"].ToString(),
                                    Nama   = reader["Nama"].ToString(),
                                    Harga  = reader["Harga"].ToString(),
                                    Satuan = reader["Satuan"].ToString(),
                                    Stok   = reader["Stok"].ToString()
                                };
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }