我正在尝试使用OleDb将一条新记录插入一个Access数据库,使用SQL命令将一个名为' tblThread' (包含讨论帖,如果你想知道的话);这是通过一个按钮来完成的,该按钮将从两个控件(都是文本框)中获取值。
如果您希望查看以下布局:https://gyazo.com/c43abf4ce055ff997b908badb57f549a 但是,点击按钮'提交讨论'后,插入新记录的控件显示错误:
https://gyazo.com/1dbdb33290649af04f092533560b1d8c
现在,这里是此按钮的Click事件的代码:
请注意:
absDefault._memberType ='老师' (在这种情况下)
private void btnCreate_Click(object sender, EventArgs e)
{
OleDbConnection objConnection = new OleDbConnection(absDefault.conString);
OleDbCommand objCommand = new OleDbCommand();
objCommand.Connection = objConnection;
if (MessageBox.Show("[Piltover]: Are you sure you would like to create this thread", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
return; // Does not execute remaining code
}
else if (txtTitle.TextLength == 0)
{
MessageBox.Show("[Piltover]: You have not created a title");
}
else if (mtxtDescription.TextLength == 0)
{
MessageBox.Show("[Piltover]: You have not added description to your thread");
}
else
{
// DBConnection class is only used within this else block and is not needed anywhere else in this form
DataSet ds; DataRow dRow;
DatabaseConnection objConnect = new DatabaseConnection(); // Instantiating an object from DBConnectionClass and checking if an identical title exist is much faster than the OLEDB process (shown within try block below)
objConnect.Connection_String = absDefault.conString;
objConnect.SQL = "SELECT * FROM tblThread"; ds = objConnect.GetConnection;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dRow = ds.Tables[0].Rows[i];
if (txtTitle.Text.ToUpper() == dRow.ItemArray.GetValue(1).ToString())
{
MessageBox.Show("[Piltover]: Thread already exist with the title name given");
return;
}
}
}
// FIX - test to see if it works
try
{
objConnection.Open();
// Insert new thread record; avoids SQL injection
objCommand.CommandText = String.Format("INSERT INTO tblThread ([Title], [Description], [ID], [Username], [TeacherBool]) VALUES (@title, @desc, @id, @username, @teacherbool)");//, absDefault.newThreadMemberType);
objCommand.Parameters.AddWithValue("@title", txtTitle.Text);
objCommand.Parameters.AddWithValue("@desc", mtxtDescription.Text);
objCommand.Parameters.AddWithValue("id", absDefault._idNumber);
if (absDefault._memberType == "Teacher")
{
currentTeacher = new csTeacher(absDefault._idNumber, "Teacher");
objCommand.Parameters.AddWithValue("@teacherbool", "True");
objCommand.Parameters.AddWithValue("@username", currentTeacher.Username);
}
else // else 'Student'
{
currentStudent = new csStudent(absDefault._idNumber, "Student");
objCommand.Parameters.AddWithValue("@teacherbool", "False");
objCommand.Parameters.AddWithValue("@username", currentStudent.Username);
}
objCommand.ExecuteNonQuery();
MessageBox.Show("[Piltover]: Thread created");
objConnection.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
}
}
我猜测问题在于[描述]属性,但是,我已将数据类型设置为长文本: https://gyazo.com/2d99c945a0a0b98a1e48e8abaf043c2f
如果您想知道我的DatabaseConnection类中包含的内容: http://pastebin.com/RQs6qPEz
我感到困惑的是,我的输入是在边界内(如果这是问题,则不超过255个字符): 例如,https://gyazo.com/c43abf4ce055ff997b908badb57f549a 正如您所看到的,“屏蔽”文本框&#39;除了标签&#39;描述&#39;包含少于255个字符的值。
我已尝试调试以尝试找到解决方案/答案。
答案 0 :(得分:2)
System.Data.OleDb
允许我们将@names用于参数(及其占位符),但忽略名称并将参数视为严格位置。因此,参数必须按照它们在命令文本中出现的顺序声明。
在命令文本中指定
... VALUES (@title, @desc, @id, @username, @teacherbool)
但是当您通过AddWithValue
创建参数时,请按以下顺序执行此操作...
@title
@desc
id
@teacherbool
@username
......这是不一样的。
您需要交换在if
块中声明@teacherbool和@username参数的顺序。