System.Data.dll c#中发生了'System.Data.OleDb.OleDbException类型的未处理异常”>

问题描述 投票:3回答:3

我已经创建了Windows窗体,该窗体的功能是将信息添加到数据库。但是,我有一个问题。当我在“产品代码”列中输入类似“ SM0001”的数字,然后按Enter键时,它将数据存储到数据库中;当我输入与之前输入的数字相同的数字时,它不会阻止用户喜欢输入的“产品代码”已存在于数据库中。因此,这是我当前的数据库(显示在系统的datagridview中):

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS96WjhSdy5wbmcifQ==” alt =“在此处输入图像描述”>

如您所见,行“ 1”和行“ 2”具有相同的“产品代码”。我的问题是:如何防止用户两次输入相同的数字?

我已经将数据库中的主键更改为“产品代码”,但是这是我得到的错误:

错误是:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

错误发生在:

cmd.ExecuteNonQuery();

关于此功能:

private void AddDatabase(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "INSERT INTO [Table] ([ProductCode], [Description], [Price]) VALUES (@ProductCode, @Description, @Price)";
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.Parameters.Add("@ProductCode", System.Data.OleDb.OleDbType.VarChar);
                    cmd.Parameters["@ProductCode"].Value = this.numericTextBox1.Text;
                    cmd.Parameters.Add("@Description", System.Data.OleDb.OleDbType.VarChar);
                    cmd.Parameters["@Description"].Value = this.textBox3.Text;
                    cmd.Parameters.Add("@Price", System.Data.OleDb.OleDbType.Integer);
                    cmd.Parameters["@Price"].Value = this.textBox4.Text;
                    cmd.ExecuteNonQuery(); // The error is here
                    if (_choice.comboBox1.Text == "English")
                    {
                        System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                        _sound.Play();
                        DialogResult _dialogResult = MessageBox.Show("Added Successfully!", "Success", MessageBoxButtons.OK);
                        if (_dialogResult == DialogResult.OK)
                        {
                            ViewDatabase(sender, e);
                            ClearTextBoxes(sender, e);
                        }
                    }
                }
                conn.Close();
            }
        }

我想当用户键入相同的“产品代码”时,将出现消息框,提示键入的“产品代码”是不允许的,因为它存在于数据库中并且没有给出错误(终止程序的运行)。] >

我该如何解决?

谢谢

您的回答将不胜感激!

我已经创建了Windows窗体,该窗体的功能是将信息添加到数据库。但是,我有一个问题。当我在“产品代码”列中键入这样的数字“ SM0001”,并且...

c# database winforms ms-access datagridview
3个回答
2
投票

您收到的错误非常正常。您不能将重复项插入主键(也不能包含“ NULL”)列。有关主键的更多信息:

W3schools - primary key


1
投票
执行INSERT之前,您可以检查数据库是否已经包含ProductCode(在表中似乎是

key

。]

0
投票
如果您的错误是system.data.dll中发生了'system.data.oledb.oledbexception'类型的未处理异常。其他信息:溢出数据库中的注释很少用于注释或数字请更改cloumn格式为更大数字---->整数从高到双战车
© www.soinside.com 2019 - 2024. All rights reserved.