更新MS Access数据库中的值

时间:2016-05-05 14:16:14

标签: c# database ms-access sql-update

我正在尝试设置一个允许用户更改MS Access数据库中库存值的系统。它应该更新在先前表格中输入的SKU的库存值。相反,我没有收到任何错误消息,该程序只是坐在那里并没有做任何事情。记录没有更新,没有抛出异常,我99%确定连接字符串是有效的。

我已对其进行了设置,以便在操作完成后,它会通过弹出窗口通知用户,但这也不会显示。任何建议将不胜感激。

代码:

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

namespace InventoryManager
{
    public partial class frmAdjustment : Form
    {
        frmAmendStock _main;

        public string enteredSKU { get; set; }

        public frmAdjustment(frmAmendStock main)
        {
            InitializeComponent();
            _main = main;
        }        

        private void frmAdjustment_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSubmit;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            using (OleDbConnection connect = new OleDbConnection())
            {

                connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Other\Documents\University Work\USB\Individual Project\Artefact\InventoryManager\InventoryManager\stock.mdb";
                connect.Open();

                OleDbCommand cmd = new OleDbCommand("UPDATE items SET Stock = @stock, Stock_Counted = @counted WHERE SKU ='" +enteredSKU+"'", connect);
                string units = txtAmount.Text;

                    if (connect.State == ConnectionState.Open)
                    {
                        if (string.IsNullOrEmpty(units))
                        {
                            MessageBox.Show("Please enter the correct amount of units.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            cmd.Parameters.Add("@stock", OleDbType.Integer, 5).Value = txtAmount.Text;
                            cmd.Parameters.Add("@counted", OleDbType.Integer, 5).Value = txtAmount.Text;

                            try
                            {
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Stock Adjusted", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                txtAmount.Clear();

                                connect.Close();
                            }
                            catch (Exception expe)
                            {
                                MessageBox.Show(expe.Source);
                                connect.Close();
                            }
                      }
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
        }
    }
}

更新代码后,我现在收到此错误:

Microsoft JET Database Engine error

2 个答案:

答案 0 :(得分:1)

执行更新的代码是 if (string.IsNullOrEmpty(units))块内部,因此如果units实际上有值,则代码将永远不会执行。看起来您的错误消息后需要else

答案 1 :(得分:0)

管理以解决问题并将其保存到数据库中。感谢大家的帮助和建议。

这是工作代码:

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

namespace InventoryManager
{
    public partial class frmAdjustment : Form
    {
        frmAmendStock _main;

        public string enteredSKU { get; set; }

        public frmAdjustment(frmAmendStock main)
        {
            InitializeComponent();
            _main = main;
        }        

        private void frmAdjustment_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSubmit;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            using (OleDbConnection connect = new OleDbConnection())
            {

                connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Other\Documents\University Work\USB\Individual Project\Artefact\InventoryManager\InventoryManager\stock.mdb";
                connect.Open();

                OleDbCommand cmd = new OleDbCommand("UPDATE items SET Stock = @stock, Stock_Counted = @counted WHERE SKU LIKE '" +enteredSKU+"'", connect);
                string units = txtAmount.Text;

                    if (connect.State == ConnectionState.Open)
                    {
                        if (string.IsNullOrEmpty(units))
                        {
                            MessageBox.Show("Please enter the correct amount of units.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            cmd.Parameters.Add("@stock", OleDbType.Integer, 5).Value = txtAmount.Text;
                            cmd.Parameters.Add("@counted", OleDbType.Integer, 5).Value = txtAmount.Text;

                            try
                            {
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Stock Adjusted", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                txtAmount.Clear();

                                connect.Close();
                            }
                            catch (Exception expe)
                            {
                                MessageBox.Show(expe.ToString());
                                connect.Close();
                            }
                      }
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
        }
    }
}