c# - 如何解决from子句中的语法错误 [英] c# - How do I solve syntax error in from clause

查看:338
本文介绍了c# - 如何解决from子句中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:



MY CODE IS :

string query="select DISPATCH.Bill_No, NWTHAN.Quality, NWTHAN.Than_No, NWTHAN.Length, NWTHAN.Weight, NWTHAN.Tobaleno "+
                           "from DISPATCH , BALES , NWTHAN"+
                           "where DISPATCH.Bill_no='" + comboBox1.SelectedValue.ToString() + "' and BALES.Bill_No=DISPATCH.Bill_no and NWTHAN.Tobaleno=BALES.Bale_No" +
                            "order by NWTHAN.Than_No";







帮助我!!!




HELP ME !!!

推荐答案

你有两个问题:

1.你的代码很宽泛n到SQL注入...永远不要使用字符串连接来创建查询,使用参数化查询!

2.你不要在查询的各个部分之间添加空格......

这个:

You have two problems:
1. Your code is wide open to SQL injection...Never use string concatenation to create queries, use parametrized queries!
2. You do not add spaces between the parts of the query...
This:
"from DISPATCH , BALES , NWTHAN"+
"where DISPATCH.Bill_no='"



等于他:


Is equal to his:

"from DISPATCH , BALES , NWTHANwhere DISPATCH.Bill_no='"



你可以看到最后一个和之间没有空格其中 ...

您在订单上的颜色与相同...


As you can see no space between the last able and the where...
You have the very same at the order by...


1.我看到的第一个错误是你忘了在之前添加WHERE子句,并且在order by之前添加空格。



2.所以你应该像这样更改你的SQL:

1.The first error that I saw is that you forgot to let a space before to add "WHERE" clause, ans also before "order by".

2.So you should change your SQL like this:
string query="select DISPATCH.Bill_No, NWTHAN.Quality, NWTHAN.Than_No, NWTHAN.Length, NWTHAN.Weight, NWTHAN.Tobaleno "+
                           "from DISPATCH , BALES , NWTHAN "+ //Here was the problem!
                           "where DISPATCH.Bill_no='" + comboBox1.SelectedValue.ToString() + "' and BALES.Bill_No=DISPATCH.Bill_no and NWTHAN.Tobaleno=BALES.Bale_No " //Also here a similar problem! 
                            +"order by NWTHAN.Than_No";


string query="select DISPATCH.Bill_No, NWTHAN.Quality, NWTHAN.Than_No, NWTHAN.Length, NWTHAN.Weight, NWTHAN.Tobaleno "+
                           "from DISPATCH , BALES , NWTHAN"+
                           " where DISPATCH.Bill_no='" + comboBox1.SelectedValue.ToString() + "' and BALES.Bill_No=DISPATCH.Bill_no and NWTHAN.Tobaleno=BALES.Bale_No" +
                            " order by NWTHAN.Than_No";





只需在where和order by子句之前添加空格。你的查询运行正常。



just add space before where and order by clause. your query will run fine.


这篇关于c# - 如何解决from子句中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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