Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#OleDb插入带参数的整数_C#_.net_Winforms_Ms Access_Oledbcommand - Fatal编程技术网

C#OleDb插入带参数的整数

C#OleDb插入带参数的整数,c#,.net,winforms,ms-access,oledbcommand,C#,.net,Winforms,Ms Access,Oledbcommand,我对C#WinForm中的insert命令有问题。我需要插入特殊字符,所以我需要参数或一些替代。但我无法将数据插入数据库。我的第二列RecordNo允许唯一,并且是一个整数。这就是我面临麻烦的地方。保存一个rec后,当我更改RecordNo并尝试保存数据时,它显示数据重复。请帮我解决我的问题 我的代码: private void saverec_Click(object sender, EventArgs e) { try {

我对C#WinForm中的insert命令有问题。我需要插入特殊字符,所以我需要参数或一些替代。但我无法将数据插入数据库。我的第二列RecordNo允许唯一,并且是一个整数。这就是我面临麻烦的地方。保存一个rec后,当我更改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();

    }

我用谷歌搜索了一下,但没用。请帮帮我。

这就是答案。我使用了globalconnectvar。我刚刚更改了它,并使用参数AddWithValue作为Add,我认为这也是一个原因

代码是:

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();

        }

首先,我建议在任何地方使用参数化SQL,而不仅仅是在一个地方。接下来,我建议在任何需要的地方创建一个新的连接和命令,使用
语句适当地关闭所有内容。接下来,我建议阅读.NET命名约定,并相应地重命名方法和变量。之后,更新问题。我怀疑问题可能是
OleDbCommand
只支持位置参数,而不支持命名参数。。。但是现在很难说。下面是你的建议,如果得到修复,答案就是。你在这里误用了
AddWithValue
AddWithValue
的第二个参数是值,而不是类型。您仍然没有为命令或连接使用
语句。您在此处误用了
AddWithValue
AddWithValue
的第二个参数是值,而不是类型。您仍然没有为命令或连接使用
语句。