Sql命令 - 未关闭标记 [英] Sql command - unclosed mark

查看:99
本文介绍了Sql命令 - 未关闭标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,我试图一次做两次插入,在这个命令中我得到并且错误

字符串后的未闭合引号')'。'







Please, im trying to do two inserts at one time, in this command i got and error
"Unclosed quotation mark after the character string ')'.'
"


cmd.CommandText = "INSERT Proby (projektId,formaId,maszynaId,detalId, celId) select Projekt.projektId, Forma.formaId, Maszyna.maszynaId, Detal_komplet.detalId, Cel.celId from Projekt, Forma,Maszyna,Detal_komplet,Cel where" +
                   " projektNazwa = '" + comboProjekt.SelectedValue.ToString() + "' and formaNazwa = '"
                   + comboForma.SelectedValue.ToString() + "' and maszynaNazwa = '"
                   + comboMaszyna.SelectedValue.ToString() + "' and detalNazwa = '"
                   + comboDetal.SelectedValue.ToString() + "' and celNazwa = '"
                   + comboCel.SelectedValue.ToString() + " & Proby(czasTrw) values('"+comboTrwanie.SelectedValue.ToString()+"')";





我的尝试:



我试图在不同的地方添加quatation标记,没有结果



What I have tried:

I tried to add quatation mark in different place, with no results

推荐答案

不要使用连接字符串来创建sql命令 - 你有风险 SQL注入 [ ^ ]攻击。



使用参数化查询 [ ^ ]为了避免这种情况,你会发现它也会修复这个特定的问题



我开始将你的查询转换为使用参数并获得到目前为止...
Don't use concatenated strings to create sql commands - you are at risk of SQL Injection[^] attack.

Use parameterised queries[^] to avoid that and you will find it will also fix this specific problem

I started to convert your query to use parameters and got this far ...
cmd.CommandText = "INSERT Proby (projektId,formaId,maszynaId,detalId, celId) select Projekt.projektId, Forma.formaId, Maszyna.maszynaId, Detal_komplet.detalId, Cel.celId from Projekt, "
     + "Forma,Maszyna,Detal_komplet,Cel where " 
     + " projektNazwa = @projectNazwa and formaNazwa = @formaNazwa and maszynaNazwa = @maszynaNazwa " 
     + " and detalNazwa = @detalNazwa and celNazwa = @celNazwa & Proby(czasTrw) values(@Trwanie)"
				   
     cmd.Parameters.AddWithValue("@projectNazwa", comboProjekt.SelectedValue.ToString());
     cmd.Parameters.AddWithValue("@formaNazwa", comboForma.SelectedValue.ToString());
     cmd.Parameters.AddWithValue("@maszynaNazwa", comboMaszyna.SelectedValue.ToString());
     cmd.Parameters.AddWithValue("@detalNazwa", comboDetal.SelectedValue.ToString());
     cmd.Parameters.AddWithValue("@celNazwa", comboCel.SelectedValue.ToString());
     cmd.Parameters.AddWithValue("@Trwanie", comboTrwanie.SelectedValue.ToString());

然后我能够在SQL结束时发现值(@Trwanie) - 这对我来说不合适,可能会导致更多问题。摆脱它。

but then I was able to spot values(@Trwanie) at the end of your SQL - that doesn't look right to me and will probably cause further problems. Get rid of it.


正如已经指出的那样,总是使用参数。但是看一下你的SQL语句,你的查询中有5个表但你没有加入它们似乎有点奇怪。您是否错过了连接条件?



另外,为了避免模糊列名,请始终在引用列时定义别名。考虑以下

As already pointed out always use parameter. But also looking at your SQL statement it seems a bit odd that you have 5 tables in your query but you don't join them . Are you missing the join conditions?

Also in order to avoid ambiguous column names, always define the aliases when referencing the columns. Consider the following
...Cel.celId 
from Projekt, Forma,Maszyna,Detal_komplet,Cel 
where projekt.projektNazwa...


尝试:

Try:
+ comboCel.SelectedValue.ToString() + "' & Proby(czasTrw) 



-----

不是你问题的解决方案,但是你有另外一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,你将使用用户输入来完成它,这打开了一个名为SQL注入的漏洞的大门,它对你的数据库很危险并且容易出错。

名称中的单引号和程序崩溃。如果用户输入Brian O'Conner之类的名称可能会导致应用程序崩溃,那么它就是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,它被提升为S.带有所有凭据的QL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

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

PHP:SQL注入 - 手册 [ ^ ]

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


-----
Not a solution to your question, but another problem you have.
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[^]


这篇关于Sql命令 - 未关闭标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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