【发布时间】: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