Esempio n. 1
0
        public Boolean NhapHang(NhapHang nh)
        {
            connect();
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }


            SqlTransaction nhaphang = con.BeginTransaction();

            try
            {
                SqlCommand insert_nh = new SqlCommand("INSERT_PNH", con, nhaphang);
                insert_nh.CommandType = CommandType.StoredProcedure;
                insert_nh.Parameters.Add("@MNCC", SqlDbType.Char).Value = nh.GetMNCC();
                insert_nh.Parameters.Add("@MNV", SqlDbType.Char).Value  = nh.GetMNV();
                nh.SetMPN((int)insert_nh.ExecuteScalar());
                foreach (SP sp in nh.GetListSP())
                {//ghi chi tiet hoa don
                    SqlCommand insert_ctnh = new SqlCommand("INSERT_CTPNH", con, nhaphang);
                    insert_ctnh.CommandType = CommandType.StoredProcedure;
                    insert_ctnh.Parameters.Add("@MPN", SqlDbType.Int).Value         = nh.GetMPN();
                    insert_ctnh.Parameters.Add("@MSP", SqlDbType.Char).Value        = sp.GetMSP();
                    insert_ctnh.Parameters.Add("@GIAMUA", SqlDbType.Money).Value    = sp.GetGiaMua();
                    insert_ctnh.Parameters.Add("@SLNHAP", SqlDbType.Int).Value      = sp.GetSoluong();
                    insert_ctnh.Parameters.Add("@THANHTIEN", SqlDbType.Money).Value = sp.GetThanhtien();
                    insert_ctnh.ExecuteNonQuery();
                }
                nhaphang.Commit();
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("loi " + ex.ToString());
                nhaphang.Rollback();
                return(false);
            }
            finally
            {
                con.Dispose();
            }
        }