c# OleDb插入带参数的整数

本文关键字:整数 参数 OleDb 插入 | 更新日期: 2023-09-27 18:04:10

我在c# WinForm中插入命令有问题。我需要插入特殊字符,所以我需要参数或其他选项。但是我不能将数据插入数据库。我的第二列RecordNo允许唯一,是一个整数。这就是我遇到麻烦的地方。当我更改RecordNo并尝试保存数据时,保存一个记录后,它显示数据是重复的。请帮我解决这个问题。

我代码:

    private void saverec_Click(object sender, EventArgs e)
    {
        try
        {
            int recva = Convert.ToInt32(recno_tb.Text);
            myconn.ConnectionString = connestr;
            dtcmd.CommandText = "INSERT INTO FormEntry (RecordNo) values ("+ recva + ")";
            dtcmd.Connection = myconn;
            myconn.Open();
            dtcmd.ExecuteNonQuery();
            myconn.Close();
            dataconnect();
            myconn.ConnectionString = connestr;
            dtcmd.CommandText = "UPDATE FormEntry SET ImageName = @imagena,EmailId = @email WHERE [RecordNo] = " + recva + "";                dtcmd.Parameters.Add("@imagena", OleDbType.VarChar).Value = imgname_tb.Text;
            //dtcmd.Parameters.AddWithValue("@recno", recno_tb.Text);
            dtcmd.Parameters.AddWithValue("@email", OleDbType.VarChar).Value = email_tb.Text;
            dtcmd.Connection = myconn;
            myconn.Open();
            dtcmd.ExecuteNonQuery();
            myconn.Close();
            dataconnect();
            addnew_Click(sender, e);
            recno_tb.Text = (recva + 1).ToString();
            email_tb.Focus();
        }
        catch (Exception ex)
        {
            if (ex.Message.ToString() == "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.")
            {
                MessageBox.Show("Record already exists. Try entering new record.'nYou can also find and edit the record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (ex.Message.ToString() == "Field 'FormEntry.RecordNo' cannot be a zero-length string.")
            {
                MessageBox.Show("Record No can't be null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        myconn.Close();
    }

我用谷歌搜索过,但没有用。所以请帮帮我。

c# OleDb插入带参数的整数

这就是答案。我是使用全局连接var.我只是改变了它,我已经使用参数AddWithValue作为添加,我认为这也是一个原因。

代码是:

private void saverec_Click(object sender, EventArgs e)
        {
            try
            {
                int recna = Convert.ToInt32(recno_tb.Text);
                OleDbConnection myconn = new OleDbConnection();
                myconn.ConnectionString = connestr;
                var insequ = "INSERT INTO FormEntry (ImageName, RecordNo, EmailId) VALUES (?,?,?)";
                OleDbCommand dtcmd = new OleDbCommand(insequ, myconn);
                dtcmd.Parameters.AddWithValue("@imagena", OleDbType.VarChar).Value = imgname_tb.Text;
                dtcmd.Parameters.AddWithValue("@recno", OleDbType.Integer).Value = recna;
                dtcmd.Parameters.AddWithValue("@email", OleDbType.VarChar).Value = email_tb.Text;
                myconn.Open();
                dtcmd.ExecuteNonQuery();
                myconn.Close();
                dataconnect();
                addnew_Click(sender, e);
                recno_tb.Text = (recna + 1).ToString();
                email_tb.Focus();
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString() == "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.")
                {
                    MessageBox.Show("Record already exists. Try entering new record.'nYou can also find and edit the record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (ex.Message.ToString() == "Field 'FormEntry.RecordNo' cannot be a zero-length string.")
                {
                    MessageBox.Show("Record No can't be null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            myconn.Close();
        }