C# OleDb 插入带参数的整数
Posted
技术标签:
【中文标题】C# OleDb 插入带参数的整数【英文标题】:C# OleDb Insert Integer with Parameter 【发布时间】:2016-08-27 08:12:57 【问题描述】:我在 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();
我用谷歌搜索过,但没有用。所以请帮助我。
【问题讨论】:
首先,我建议everywhere 使用参数化SQL,而不仅仅是在一个地方。接下来,我建议在您需要的任何地方创建一个新的连接和命令,并使用using
语句适当地关闭所有内容。接下来,我建议阅读 .NET 命名约定,并相应地重命名您的方法和变量。之后,更新问题。我怀疑问题可能是OleDbCommand
只支持位置参数,不支持命名参数......但现在很难说。
感谢您的建议,如果在下面得到修复就是答案。
【参考方案1】:
这就是答案。我正在使用全局连接变量。我刚刚改了,我用了参数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();
【讨论】:
您在这里误用了AddWithValue
- AddWithValue
的第二个参数是值,而不是类型。您仍然没有使用using
语句来执行命令或连接。
您在这里误用了AddWithValue
- AddWithValue
的第二个参数是值,而不是类型。您仍然没有使用 using
语句来执行命令或连接。以上是关于C# OleDb 插入带参数的整数的主要内容,如果未能解决你的问题,请参考以下文章