OledbException - 字段太小而无法接受数据量
OledbException - Field too small to accept amount of data
我正在尝试使用 OleDb 通过 SQL 命令将新记录插入到名为 'tblThread' 的实体(包含讨论帖子,如果您想知道的话)中;这是通过一个按钮完成的,该按钮将从两个控件(都是文本框)中获取值。
这里是布局,如果你想看:https://gyazo.com/c43abf4ce055ff997b908badb57f549a
但是,在单击按钮 'Submit Discussion' 后,插入新记录的控件出现错误显示:
https://gyazo.com/1dbdb33290649af04f092533560b1d8c
下面是这个按钮的 Click 事件的代码:
请注意:
absDefault._memberType = 'Teacher'(在这种情况下)
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());
}
}
我猜问题出在属性 [Description] 上,不过,我已将数据类型设置为长文本:
https://gyazo.com/2d99c945a0a0b98a1e48e8abaf043c2f
如果您想知道我的数据库连接中包含什么 class:
http://pastebin.com/RQs6qPEz
让我感到困惑的是,我的输入是在边界内的(少于 255 个字符,如果这是问题所在):
例如,https://gyazo.com/c43abf4ce055ff997b908badb57f549a
如您所见,除了标签 'Description' 之外的 'masked textbox' 包含小于 255 个字符的值。
我已经尝试调试以找到solution/answer。
System.Data.OleDb
允许我们对参数(及其占位符)使用@names,但它 忽略 名称并将参数视为严格的位置。因此必须按照它们在命令文本中出现的相同顺序声明参数。
在您指定的命令文本中
... VALUES (@title, @desc, @id, @username, @teacherbool)
但是当您通过 AddWithValue
创建参数时,您会按以下顺序创建参数...
@title
@desc
id
@teacherbool
@username
...这是不一样的。
您需要交换在 if
块中声明 @teacherbool 和 @username 参数的顺序。
我正在尝试使用 OleDb 通过 SQL 命令将新记录插入到名为 'tblThread' 的实体(包含讨论帖子,如果您想知道的话)中;这是通过一个按钮完成的,该按钮将从两个控件(都是文本框)中获取值。
这里是布局,如果你想看:https://gyazo.com/c43abf4ce055ff997b908badb57f549a 但是,在单击按钮 'Submit Discussion' 后,插入新记录的控件出现错误显示:
https://gyazo.com/1dbdb33290649af04f092533560b1d8c
下面是这个按钮的 Click 事件的代码:
请注意:
absDefault._memberType = 'Teacher'(在这种情况下)
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()); } }
我猜问题出在属性 [Description] 上,不过,我已将数据类型设置为长文本: https://gyazo.com/2d99c945a0a0b98a1e48e8abaf043c2f
如果您想知道我的数据库连接中包含什么 class: http://pastebin.com/RQs6qPEz
让我感到困惑的是,我的输入是在边界内的(少于 255 个字符,如果这是问题所在): 例如,https://gyazo.com/c43abf4ce055ff997b908badb57f549a 如您所见,除了标签 'Description' 之外的 'masked textbox' 包含小于 255 个字符的值。
我已经尝试调试以找到solution/answer。
System.Data.OleDb
允许我们对参数(及其占位符)使用@names,但它 忽略 名称并将参数视为严格的位置。因此必须按照它们在命令文本中出现的相同顺序声明参数。
在您指定的命令文本中
... VALUES (@title, @desc, @id, @username, @teacherbool)
但是当您通过 AddWithValue
创建参数时,您会按以下顺序创建参数...
@title
@desc
id
@teacherbool
@username
...这是不一样的。
您需要交换在 if
块中声明 @teacherbool 和 @username 参数的顺序。