C#SQL更新未更新但不能添加任何错误 [英] C# SQL update not updating but can add no errors

查看:71
本文介绍了C#SQL更新未更新但不能添加任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我已经玩了好几个小时了,它应该在我的意见中工作但不是这样我很安静,

我目前只是想更新一个记录SQL数据库。我可以添加一条记录并删除一条记录就好了,但是当我去更新时,我没有错误,但它只是没有更新。

我必须遗漏一些小而简单的东西。请告诉我忘记了什么

非常感谢你!

这是我所做的和编写的小时试验和错误



Hello all I have been playing with this for hours and it should work in my opinion but it doesn’t so I am quiet confused,
I am currently just trying to update a record in a SQL database.I can add a record and delete a record just fine but when I go to update I get no errors but it just doesn’t update.
I must be missing something small and simple. Please show me what I am forgetting
Thank you very much!
Here is what I have and done coding for hours trial and error

private void btnAddProd_Click(object sender, RoutedEventArgs e)
        {//validate method   // product = new Product(txtProductName.Text, Convert.ToDecimal(txtProductCost.Text), txtProductType.Text);
            product = new Product();
            MakeProduct(product);
            if(modify)
            {
                Product theOneAndOnly = new Product();
                theOneAndOnly.ProductID = product.ProductID;
                MakeProduct(theOneAndOnly);
                try
                {             
                    ProductDBmethods.UpdateProduct(product, theOneAndOnly);
                    
                }
                catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
            }
            else
            {
                ProductDBmethods.CreateProduct(product);
            }
        private void MakeProduct(Product p)
        {
            p.ProductName = txtProductName.Text;
            p.ProductCost = Convert.ToDecimal(txtProductCost.Text);
            p.ProductType = txtProductType.Text;
        }
	        public static void UpdateProduct(Product product, Product newprod)
        {
            string SQLupdateStatement = "UPDATE Products SET " +
                "ProductName = @newProductName, ProductCost = @newProductCost, ProductType = @newProductType " +
                "WHERE ProductName = @ProductName AND ProductCost = @ProductCost AND ProductType = @ProductType";
            SqlCommand cmd = new SqlCommand(SQLupdateStatement, connection);
            cmd.Parameters.AddWithValue("@ProductName", product.ProductName);
            cmd.Parameters.AddWithValue("@ProductCost", product.ProductCost);
            cmd.Parameters.AddWithValue("@ProductType", product.ProductType);
            cmd.Parameters.AddWithValue("@newProductName", newprod.ProductName);
            cmd.Parameters.AddWithValue("@newProductCost", newprod.ProductCost);
            cmd.Parameters.AddWithValue("@newProductType", newprod.ProductType);
            try
            {
                connection.Open();
                cmd.ExecuteNonQuery();
            }
            catch(SqlException sql)
            {
                sql.Message.ToString();
                throw sql;
            }
            catch(Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
                connection.Close();
            }
        }
        public static void CreateProduct(Product product)
        {
            string SQLcreateStatement = "INSERT INTO Products(ProductName, ProductCost, ProductType) VALUES(@ProductName, @ProductCost, @ProductType)";
            SqlCommand createCommand = new SqlCommand(SQLcreateStatement, connection);
            createCommand.Parameters.AddWithValue("@ProductName", product.ProductName);
            createCommand.Parameters.AddWithValue("@ProductCost", product.ProductCost);
            createCommand.Parameters.AddWithValue("@ProductType", product.ProductType);
            try
            {
                connection.Open();
                createCommand.ExecuteNonQuery();
            }
            catch (SqlException sql)
            {
                sql.Message.ToString();
                throw sql;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
                connection.Close();
            }
        }
private void btnModifyProduct_Click(object sender, RoutedEventArgs e)
        {
            int selectedProduct = lstProducts.SelectedIndex;
            if(selectedProduct != -1)
            {
                Product product = (Product)currentProducts[selectedProduct];
                string message = "Are you sure you want to edit: " + product.ProductName + "?";
                MessageBoxResult modify = MessageBox.Show(message, "Accept Modify", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if(modify == MessageBoxResult.Yes)
                {
                    Window modifyProduct = new ProductAdd(product);
                    modifyProduct.Show();    

                }
            }
        }

推荐答案

在调用ProductDBmethods之前查看我的内联注释.UpdateProduct



View my inline comment before calling ProductDBmethods.UpdateProduct

private void btnAddProd_Click(object sender, RoutedEventArgs e)
        {
            product = new Product();
            MakeProduct(product);
            if(modify)
            {
                Product theOneAndOnly = new Product();
                theOneAndOnly.ProductID = product.ProductID;
                MakeProduct(theOneAndOnly);
                try
                {             
// The problem lies here.. All values in 'product' and 'theOneAndOnly' are equal.. so the update is taking place, but you are setting the same values!
                    ProductDBmethods.UpdateProduct(product, theOneAndOnly);
                    
                }
                catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
            }
            else
            {
                ProductDBmethods.CreateProduct(product);
            }
        private void MakeProduct(Product p)
        {
            p.ProductName = txtProductName.Text;
            p.ProductCost = Convert.ToDecimal(txtProductCost.Text);
            p.ProductType = txtProductType.Text;
        }






这里你犯了一个错误。在下面一行你已经使用了代码如



Hi,

Here you are doing a mistake. In the following line you have used the code like

Quote:

product =新产品();

MakeProduct(产品);

product = new Product();
MakeProduct(product);





并在 MakeProduct()您正在创建Product类的对象并在其中指定一些值的方法。如果你不会在 MakeProduct()方法中返回你指定的对象,那么ProductClass对象将如何包含调用函数中的值。



MakeProduct()方法返回相同的产品,并在调用函数中使用该方法。然后,只有您才能在下面的4行中获得要更新的产品的详细信息。



And in the "MakeProduct()" method you are creating an object of the "Product" class and assigning some values there. If you will not return the object that you are assigning in the MakeProduct() method then how the ProductClass object will contain the values there in the calling function.

Return the same product from the MakeProduct() method and use that method in the calling function. Then only you will be able to get the details of the product to update in the4 following line.

Quote:

ProductDBmethods.UpdateProduct(product,theOneAndOnly);

ProductDBmethods.UpdateProduct(product, theOneAndOnly);



您的代码应如下所示:


Your code should be something like the following:

Product theOneAndOnly = new Product();
                theOneAndOnly.ProductID = product.ProductID;
                theOneAndOnly = MakeProduct(theOneAndOnly);



注意:您的返回类型应为 MakeProduct()as 产品类类型。

希望您了解更新失败的原因。


谢谢

Sisir


Note: You should have the return type of "MakeProduct()" as Product class type.
Hope you understand the reason of failure in updating.

Thanks
Sisir


这篇关于C#SQL更新未更新但不能添加任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆