MS Access 的 INSERT INTO 语句中的语法错误
我有一个在正常条件下工作的 SQL Insert Into 命令。这意味着如果我填写每个文本框,数据将发送到数据库(Access db)。
但是,当我“忘记”1 个文本框时,我收到“INSERT INTO 语句中的语法错误”。 如何避免这种情况?
string commandPerson = "Insert into Person (LastName,FirstName,DateOfBirth,Phone,Email,AdditionalInfo, Hobbies, CVinDropBOX, Informationrequest) values('" + txtLastN.Text + "','" + txtFirstN.Text + "'," + txtDOB.Text + ",'" + txtPhone.Text + "','" + txtEmail.Text + "','" + txtAdditionalInfo.Text + "','" + txtHobbies.Text + "'," + chkCVDROPBOX.Checked + "," + chkInformation.Checked + ")";
当每个文本框都有一个值时,就没有问题。 仅当我将 1 或 2 个文本框留空时,错误消息才会显示: INSERT INTO 语句中的语法错误
I have a SQL Insert Into command that works in normal conditions. That means if I fill in every textbox, the data is send to the db (Acces db).
But when I 'forget' 1 textbox, I receive a "Syntax error in INSERT INTO statement."
How can you avoid this?
string commandPerson = "Insert into Person (LastName,FirstName,DateOfBirth,Phone,Email,AdditionalInfo, Hobbies, CVinDropBOX, Informationrequest) values('" + txtLastN.Text + "','" + txtFirstN.Text + "'," + txtDOB.Text + ",'" + txtPhone.Text + "','" + txtEmail.Text + "','" + txtAdditionalInfo.Text + "','" + txtHobbies.Text + "'," + chkCVDROPBOX.Checked + "," + chkInformation.Checked + ")";
When every textbox has a value, there is no problem.
It is only when i leave 1 or 2 textboxes empty, the error message shows :
Syntax error in INSERT INTO statement
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用参数化方法,不仅可以安全地防止 SQL 注入,还可以让您解决问题,因为在未提供参数值时,您会将参数值设置为 NULL(或 string.empty)。
这里是一个例子:
这里是完整的文章: http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access
use a parametrized approach which not only is safe against SQL Injection, but also let's you solve your problem because you will set a parameter value to NULL (or string.empty) when not provided.
here an example:
and here the full article: http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access
尝试对 dob 执行此操作(您缺少单引号):
try this for the dob (you are missing the single quotes):