我在','附近的语法不正确。 [英] I got an incorrect syntax near ', '.

查看:87
本文介绍了我在','附近的语法不正确。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入我的数据库中的表,并且在','附近出现错误语法错误。以下是我的代码



我尝试过:



< pre> con = New SqlConnection(cs)
con.Open()
Dim cb1 As String =insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes)VALUES( & txtBillID.Text&,'& txtBillNo.Text&',& txtCheckInID.Text&,@ d2,@ d1)
cmd =新的SqlCommand(cb1)
cmd.Connection = con
cmd.Parameters.AddWithValue(@ d1,txtNotes.Text)
cmd.Parameters.AddWithValue(@ d2,dtpBillDate.Value)
cmd.ExecuteReader()
con.Close()

解决方案

无法知道你的查询究竟是什么,因为它取决于用于构建查询的值。

只有调试器才能显示真正的查询,然后我们可以说出问题是什么。

  Dim  cb 1 作为 字符串 =  插入CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes)VALUES(& txtBillID.Text&  ,'& txtBillNo.Text&  ',& txtCheckInID.Text&  ,@ d2,@ d1) 



你很高兴在同一个查询中混合连接和参数,这很糟糕。



永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]



次要问题,不可能知道你的查询究竟是什么,因为它取决于值参数。

只有调试器才能显示真正的查询。


为什么不保持干净而不是混合参数化和常规查询字符串连接?坚持参数化。这将为您节省一些像SQL注入漏洞一样的头痛并避免语法问题。



另一个观察,看起来像txtBillID,txtBillNo,txtCheckInID应该是整数,您可能需要更新代码才能在将字符串插入表格之前将其转换为整数。



  Dim  cb1 作为 字符串 =  插入CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5) 
cmd = SqlCommand(cb1)
cmd.Connection = con
cmd.Parameters.AddWithValue( @ d1,txtBillID.Text)
cmd.Parameters.AddWithValue( @ d2,tx tBillNo.Text)
cmd.Parameters.AddWithValue( @ d3,txtCheckInID.Text )
cmd.Parameters.AddWithValue( @ d4,dtpBillDate.Value)
cmd.Parameters.AddWithValue( @ d5,txtNotes.Text)


am trying to insert into a table in my database and i get this error of incorrect syntax near ','. below is my code

What I have tried:

<pre>con = New SqlConnection(cs)
            con.Open()
            Dim cb1 As String = "insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes ) VALUES (" & txtBillID.Text & ",'" & txtBillNo.Text & "'," & txtCheckInID.Text & ",@d2,@d1)"
            cmd = New SqlCommand(cb1)
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@d1", txtNotes.Text)
            cmd.Parameters.AddWithValue("@d2", dtpBillDate.Value)
            cmd.ExecuteReader()
            con.Close()

解决方案

It is impossible to know what is exactly your query because it depend on the values used to build the query.
Only the debugger can show what is the real query, then we can say what is the problem.

Dim cb1 As String = "insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes ) VALUES (" & txtBillID.Text & ",'" & txtBillNo.Text & "'," & txtCheckInID.Text & ",@d2,@d1)"


You are happily mixing concatenation and parameters in same query, that is bad.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

Secondary problem, it is impossible to know what is exactly your query because it depend on the values of the parameters.
Only the debugger can show what is the real query.


Why not keep it clean instead of mixing the Parameterized and regular query with string concatenation? stick with Parameterized. This will save you some headache down the road like SQL injection vulnerability and avoid the syntax issue.

Another observation, look like the txtBillID,txtBillNo,txtCheckInID should be integers, you might need to update the code to convert the string into integer before inserting it into the table.

Dim cb1 As String = "insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes ) VALUES (@d1, @d2, @d3, @d4,@d5)"
        cmd = New SqlCommand(cb1)
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@d1", txtBillID.Text)
        cmd.Parameters.AddWithValue("@d2", txtBillNo.Text)
        cmd.Parameters.AddWithValue("@d3", txtCheckInID.Text)
        cmd.Parameters.AddWithValue("@d4", dtpBillDate.Value)
        cmd.Parameters.AddWithValue("@d5", txtNotes.Text)


这篇关于我在','附近的语法不正确。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆