示例#1
0
        public bool AddStoreProduct(StoreProduct storeProduct)
        {
            MySqlConnection cnn = DBUtility.getConnection();
            if (cnn != null)
            {
                cnn.Open();

                MySqlCommand cmd_checkstock = new MySqlCommand("SELECT COUNT(*) FROM StoreProduct WHERE Storeid=@storeid AND productid=@Productid", cnn);
                cmd_checkstock.Prepare();
                cmd_checkstock.Parameters.AddWithValue("@storeid", storeProduct.StoreId);
                cmd_checkstock.Parameters.AddWithValue("@productid", storeProduct.ProductId);
                int record = int.Parse(cmd_checkstock.ExecuteScalar().ToString());
                if (record > 0)
                {
                    return false;
                }

                MySqlTransaction transaction = cnn.BeginTransaction();
                try
                {
                    const string SQL = @"INSERT INTO 
											storeproduct(
												storeid
                                                , productid
                                                , quantity
                                                , returnquantity
											) 
										VALUES(
											@storeid
                                            , @productid
                                            , @quantity
                                            , @returnquantity
										);";
                    MySqlCommand command = new MySqlCommand(SQL, cnn);
                    command.Prepare();
                    command.Parameters.AddWithValue("@storeid", storeProduct.StoreId);
                    command.Parameters.AddWithValue("@productid", storeProduct.ProductId);
                    command.Parameters.AddWithValue("@quantity", storeProduct.Quantity);
                    command.Parameters.AddWithValue("@returnquantity", storeProduct.ReturnQuantity);
                    if (command.ExecuteNonQuery() > 0)
                    {
                        transaction.Commit();
                        return true;
                    }
                }
                catch (MySqlException e)
                {
                    Console.WriteLine(e);
                    transaction.Rollback();
                }
                finally
                {
                    cnn.Close();
                }
            }
            return false;
        }
示例#2
0
        public bool UdateStoreProduct(StoreProduct storeProduct)
        {
            MySqlConnection cnn = DBUtility.getConnection();
            if (cnn != null)
            {
                try
                {
                    cnn.Open();
                    const string SQL = @"UPDATE 
											storeproduct 
										SET 
                                              quantity = @quantity
                                            , returnquantity = @returnquantity 
										WHERE 
											storeid = @storeid AND productid = @productid";
                    MySqlCommand command = new MySqlCommand(SQL, cnn);
                    command.Prepare();
                    command.Parameters.AddWithValue("@storeid", storeProduct.StoreId);
                    command.Parameters.AddWithValue("@productid", storeProduct.ProductId);
                    command.Parameters.AddWithValue(@"quantity", storeProduct.Quantity);
                    command.Parameters.AddWithValue(@"returnquantity", storeProduct.ReturnQuantity);
                    if (command.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                }
                catch (MySqlException e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    cnn.Close();
                }
            }
            return false;
        }