【问题标题】:Updating the value(s) in an MS Access database更新 MS Access 数据库中的值
【发布时间】:2016-05-05 14:16:14
【问题描述】:

我正在尝试建立一个允许用户更改 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");
                }
            }
        }
    }
}

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

【问题讨论】:

  • 您能提供更多信息吗?记录是否在 Access 数据库中更新?你的连接字符串有效吗?有什么例外吗?
  • 记录没有更新,连接字符串有效,没有抛出异常。
  • 数据类型呢?我看到它们是 VarChar 类型 - 它们在您的数据库中是什么(文本)?
  • 将它们更改为整数并没有发生任何事情,按一次提交按钮什么也没做,第二次按它但是会出现错误An unhandled exception of type 'System.InvalidOperationException' occured in System.Data.dll Additional information: Not allowed to change the 'ConnectionString propety. The connection's current state is open.
  • connect 是什么?执行完 SQL 语句后,您将需要使用 using 语句来处理连接

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


【解决方案1】:

您执行更新的代码if (string.IsNullOrEmpty(units)) 块内,所以如果units 实际上有一个值,那么代码将永远不会被执行。您的错误消息后似乎需要else

【讨论】:

  • 我已经修改了修复这个问题的代码,现在我只收到一个弹出窗口说“Microsoft JET 数据库引擎”。
  • @iiAaronXiX MessageBox.Show(expe.ToString());
  • @iiAaronXiX - 不要将新信息隐藏在 cmets 中。编辑您的问题。
【解决方案2】:

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

这是工作代码:

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");
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    相关资源
    最近更新 更多