[英]Save function in c# windows form not completing due to a syntax error
I am struggling to get this function working as it keeps flagging a syntax error within the INSERT INTO
statement however the same code works on another page / form... can anyone help me out? 我正在努力使此函数正常工作,因为它会在INSERT INTO
语句中不断标记语法错误,但是同一代码可在另一页/窗体上工作……有人可以帮我吗?
It seems to have this issue on another form now too however I can't see what is wrong with it... 看来现在也存在其他形式的问题,但是我看不出这是怎么回事...
private void btnAssign_Click(object sender, EventArgs e)
{
string LocID = txtLocID.Text;
string AssID = txtAssID.Text; // declaing instance names
string StaID = txtID.Text;
string Sku = txtSKU.Text;
if (txtLocID.Text.Length < 1) // validating a text box
{
MessageBox.Show("Please Enter an Location ID");
}
else if (txtAssID.Text.Length < 1)
{
MessageBox.Show("Please Enter an Assignment ID");
}
else if (txtID.Text.Length < 1)
{
MessageBox.Show("Please Enter a Staff ID");
}
else if (txtSKU.Text.Length < 1)
{
MessageBox.Show("Please Enter a Product SKU");
}
else
{
try
{
int Loc;
Cons.ConnectionString = ConnectionDetail.Warehouse;
OleDbCommand cmds = new OleDbCommand();
Cons.Open();
cmds.CommandText = @"Insert into Assign (Location ID, Assign ID, Staff ID, SKU) VALUES (@LocID, @AssID, @StaID, @Sku)";
cmds.Connection = Cons;
cmds.Parameters.AddWithValue("LocID", LocID);
cmds.Parameters.AddWithValue("AssID", AssID);
cmds.Parameters.AddWithValue("StaID", StaID);
cmds.Parameters.AddWithValue("Sku", Sku);
Loc = cmds.ExecuteNonQuery();
if (Loc > 0)
MessageBox.Show("Location has successfully been assigned a product and Saved");
}
catch (Exception err)
{
MessageBox.Show("error:" + Environment.NewLine + err.Message);
}
finally
{
if (Cons.State == ConnectionState.Open)
Cons.Close();
}
}
}
When your column names contains spaces you need to encapsulate them between square brackets to avoid confusing the database parser 当您的列名称包含空格时,您需要将其封装在方括号之间,以避免混淆数据库解析器
cmds.CommandText = @"Insert into Assign ([Location ID], [Assign ID],
[Staff ID], SKU) VALUES (@LocID, @AssID, @StaID, @Sku)";
I also suggest to avoid using AddWithValue
because it could have unexpected side effects See Can we stop using AddWithValue already? 我还建议避免使用AddWithValue
因为它可能会产生意外的副作用。请参阅我们可以停止使用AddWithValue吗?
For example: I suppose that Location ID is a numeric field (probably an integer). 例如:我假设位置ID是一个数字字段(可能是整数)。 AddWithValue doesn't know this. AddWithValue不知道这一点。 It sees a string passed as its parameter value. 它看到传递的字符串作为其参数值。 So the parameter is passed as a string. 因此参数作为字符串传递。 The Database engine need to convert it back to the type expected by the field and this conversion could be done correctly (as probably happens with a simple conversion from a string containing only digits to an integer) or could produce an invalid value as it happens a lot with DateTime values. 数据库引擎需要将其转换回字段期望的类型,并且此转换可以正确完成(可能是从仅包含数字的字符串到整数的简单转换而发生的),或者在发生这种情况时可能产生无效值DateTime值很多。
It is better to be explicit 最好是明确的
int locID;
if(!Int32.TryParse(txtLocID.Text, out locID))
MessageBox("Not a valid location number!");
......
cmds.Parameters.Add("LocID", OleDbType.Integer).Value = locID;
INSERT into Assign([Location ID], [Assign ID] ...) values ...
在sql中,如果列/表/ ...名称包含空格或其他特殊字符,则必须将其放在方括号中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.