没有数据库更新 [英] No Updates To Database

查看:65
本文介绍了没有数据库更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要帮助,它是用VS C#2012编写的小应用程序。

当它运行应用程序时它运行正常,更新数据网格,但如果我重新启动应用程序,我导入的值不存在。

这是类:

Hi All,
I need help, it is small application written in VS C# 2012.
When it is run application it''s working properly, update datagrid, but if I restart application, my values which I import are not there.
this is the classes:

public Boolean modifyOrder(Order order)
        {
            SqlConnection connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFileName=|DataDirectory|\\dbCutting.mdf;Integrated Security=True;User Instance=True; MultipleActiveResultSets=True");
            try
            {
                string query = "update orders set orderCode =''" + order.OrderCode + "'', Style = ''" + order.Style + "'', VariantCode = ''" + order.VariantCode + "'', Color = ''" + order.Color + "'', CuttedQty = ''" + order.CuttedQty + "'', OrderQty = ''" + order.OrderQty + "'', RemainigQty = ''" + order.RemaingQty + "'' where OrderId = ''" + order.OrderId + "''";
                connection.Open();
                SqlCommand command = new SqlCommand(query, connection);
                command.ExecuteNonQuery();
                return true;

            }
            catch (Exception)
            {
                return false;
            }
            finally
            {

                connection.Dispose();
                connection.Close();
            }
        }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cutting
{
    class OrderBL
    {
        public string message = "";

        public List<order> loadOrders()
        {
            OrderDAL dal = new OrderDAL();
            return dal.loadOrders();
        }

        public Boolean modifyOrder(Order order)
        {
            OrderDAL dal = new OrderDAL();
            return dal.modifyOrder(order);
        }
    }
}



表格:


Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cutting
{
    public partial class Form1 : Form
    {
        List<order> orders = new List<order>();
        int currentIndex = -1;
        public Form1()
        {
            InitializeComponent();
            dgCutting.Columns.Add("OrderId", "ID");
            dgCutting.Columns.Add("Style", "Model");
            dgCutting.Columns.Add("VariantCode", "Variant-Code");
            dgCutting.Columns.Add("Color", "Boja");
            dgCutting.Columns.Add("OrderQty", "Potrebna Kolicina");
            dgCutting.Columns.Add("CuttedQty", "Iskrojena Kolicina");
            dgCutting.Columns.Add("RemainingQty", "Preostala kolicina za krojenje");

            dgCutting.AllowUserToAddRows = false;
            dgCutting.AllowUserToDeleteRows = false;

            OrderBL bl = new OrderBL();
            orders = bl.loadOrders();
            if (orders.Count &gt; 0)
            {
                currentIndex = 0;
            }

            FillTabel();
        }
        private void FillTabel()
        {
            dgCutting.Rows.Clear();
            if (orders.Count &gt; 0)
            {
                foreach (Order order in orders)
                {
                    if (order.RemaingQty != null)
                    {
                        int i = (int)order.OrderQty - (int)order.CuttedQty;
                        order.RemaingQty = i;
                    }

                    dgCutting.Rows.Add(order.OrderId, order.Style, order.VariantCode, order.Color, order.OrderQty, order.CuttedQty, order.RemaingQty);
                }
                dgCutting.Rows[0].Cells[0].Selected = false;
                dgCutting.Rows[currentIndex].Selected = true;
                
                txtCutted.Text = null;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                int i = dgCutting.SelectedRows[0].Index;
                //MessageBox.Show(i.ToString());

                if (int.Parse(txtCutted.Text) &lt; 0)
                {
                    MessageBox.Show("Nemoguce je uneti negativan broj", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {

                    
                    if (orders[i].CuttedQty == null)
                    {
                        orders[i].CuttedQty = Convert.ToInt32(txtCutted.Text);
                    }
                    else
                    {
                        orders[i].CuttedQty += Convert.ToInt32(txtCutted.Text);
                    }

                    OrderBL bl = new OrderBL();
                    if (bl.modifyOrder(orders[i]))
                        bl.loadOrders();
                    FillTabel();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }
    }
}





2年前的同样方式我在VS 2008上创建了类似的应用程序并且它已经工作了。

PS我为db而不是复制,也尝试了复制总是和结果是一样的。

当我用brekapoint运行没有错误总是我返回true。

任何帮助它会很棒。

先谢谢





代码块添加,虚假大胆删除 - OriginalGriff [/ edit]



On same way 2 years ago I create similarly app on VS 2008 and it was worked.
P.S. I put for db not copy, tried also with copy always and results are the same.
When I run with brekapoint no errors always I got return true.
Any help it will be great.
Thanks in Advance


[edit]Code blocks added, spurious bold removed - OriginalGriff[/edit]

推荐答案

您需要做的第一件事就是查看您的数据库。

检查是否存在已存在的行 OrderId 的值与UPDATE查询中的值相匹配(并帮自己一个忙 - 如果使用参数化查询,读取这些内容要容易得多:

The first thing you need to do is look at your database.
Check that a row exists which has the OrderId value which matches that in your UPDATE query (and do yourself a favour - it''s a lot easier to read these things if you use Parametrized queries:
string query = "UPDATE orders SET orderCode=@OC, Style=@ST, VariantCode=@VC, Color=@CL, CuttedQty=@CQ, OrderQty=@OQ, RemainigQty=@RQ WHERE OrderId=@ID";
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@OC", order.OrderCode);
command.Parameters.AddWithValue("@ST", order.Style);
command.Parameters.AddWithValue("@VC", order.VariantCode);
command.Parameters.AddWithValue("@CL", order.Color);
command.Parameters.AddWithValue("@CQ", order.CuttedQty);
command.Parameters.AddWithValue("@OQ", order.OrderQty);
command.Parameters.AddWithValue("@RQ", order.RemaingQty);
command.Parameters.AddWithValue("@ID", order.OrderId);
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
command.ExecuteNonQuery();

如果您的任何字段是用户可以输入的字符串,它还有助于保护您。 SQL注入不是一个笑话!)



因此,首先使用上面的代码参数化您的查询,然后使用断点来检查ID值的确切内容。如果看起来不错,那么在ExecuteNonQuery行中添加一个整数,并检查SQL返回的数字 - 如果是1,那么正确的行数已更新 - 零是没有行,更多的那个意味着你的数据库不是你的认为这是......

It also helps protect you in future if any of your fields are strings that users can enter. SQL Injection is not a joke!)

So, start by parameterizing your query with the code above, then use a breakpoint to examine exactly what goes in as the ID value. If that looks good, then add an integer to the ExecuteNonQuery line and check the number SQL returns - if it is one, then the right number of rows was updated - zero is no rows, and more that one means your database is not what you think it is...


这篇关于没有数据库更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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