关键字'union'附近的语法不正确。在C#中 [英] Incorrect syntax near the keyword 'union'. in C#

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

问题描述

private void FRM_RPT_Car_Details_Date_end_Load(object sender, EventArgs e)
        {
            s2 = "select Cars.Car_id,Job_Card,convert(varchar, Change_Date,101) as Change_date,convert(varchar,Reload_License.Car_E_date_Licen,101) as Car_E_date_Licen, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(Current_dist - Prev_dist) as Change_dist,Value_Fuel as Value,Qty from Full_Fuel inner join cars on Full_Fuel.Car_id= Cars.Car_id inner join Reload_License  on Cars.Car_id = Reload_License.Car_id inner join Service_Type on Full_Fuel.Servce_id = Service_Type.Servce_id where Reload_License.Car_E_date_Licen = '" + x + "';";
            s1 = "select Cars.Car_id,Job_Card,convert(varchar, Change_date,101) as Change_date,convert(varchar,Reload_License.Car_E_date_Licen,101) as Car_E_date_Licen, Service_Type.Servce_desc,Current_Dist as Current_dist,Prev_Dist as Prev_dist,(Current_dist - Prev_Dist) as Change_dist,Value_Oil as Value,QTY as Qty from Change_Oil inner join cars on Change_Oil.Car_id= Cars.Car_id inner join Reload_License  on Cars.Car_id = Reload_License.Car_id inner join Service_Type on Change_Oil.Servce_id = Service_Type.Servce_id where Reload_License.Car_E_date_Licen = '" + x + "'; ";
            s = "select Cars.Car_id,Job_card,convert(varchar, Change_date,101) as Change_date ,convert(varchar,Reload_License.Car_E_date_Licen,101) as Car_E_date_Licen, Service_Type.Servce_desc, Current_dist,Prev_distance as Prev_dist,(Current_dist - Prev_distance) as Change_dist,value_Tashhem as Value,NULL as [Qty] from Tashhem inner join cars on Tashhem.Car_id= Cars.Car_id inner join Service_Type on Tashhem.Servce_id = Service_Type.Servce_id inner join Reload_License on Cars.Car_id = Reload_License.Car_id where Reload_License.Car_E_date_Licen = '" + x + "' union " + s1 + " union "+ s2 +";";
            DataSet_Car_Details_end_date ds = new DataSet_Car_Details_end_date();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(s, con);
            dataAdapter.Fill(ds.Tables["Car_End_date"]);
            CrystalReport_Car_Details_date_end report = new CrystalReport_Car_Details_date_end();
            report.SetDataSource(ds.Tables["Car_End_date"]);
            crystalReportViewer_Car_Details_Date_end.ReportSource = report;
            crystalReportViewer_Car_Details_Date_end.Refresh(); 
        }

推荐答案

这是一个非常非常容易让您自己解决的问题。做Andy建议并将整个sql(变量s中的内容)粘贴到Sql Management Studio中作为新查询,然后你就能看到问题所在。



我们看不出结果是什么,所以我们无法告诉你什么是错的。另外,我们无法看到x是什么,所以我们不知道你做错了什么。



然而,你应该能够比它更快地解决它在这里发布问题。



提示:不要放;在正在联合的sql语句的末尾。事实上,我不知道使用的理由;。
This is a very, very easy issue for you to solve on your own. Do as Andy suggested and paste the entire sql (what's in your variable s) into Sql Management Studio as a new query and then you'll be able to see what the issue is.

We can't see what the result is so we can't tell you what is wrong. Also, we can't see what x is so we have no idea what you have done wrong.

However, you should be able to solve it faster than it takes to post the question here.

Hint: Don't put ; at the end of sql statements that are being unioned. In fact, I don't know of a reason to ever use ;.


看来你为x传递的任何值都会破坏SQL。你需要检查一下以确保它不会破坏你的SQL。



除此之外,C#和SQL似乎没问题。
It appears that whatever value you pass in for x breaks the SQL . You need to check that to make sure it doesn't break your SQL.

Apart from that you C# and SQL seems to be fine.


您可以在执行之前逐步执行代码并检查 s 的值。



构建和调试(Visual C#) [ ^ ]



这对调试非常有用。



好​​的。所以我会给你一个提示。



有两种方法可以在T-SQl中结束语句。关键字 GO 和半冒号; 。它们的使用略有不同,但这并不重要。



所以你有 s1 s2 ,其语法为[some SQL];。我确定你测试了这些,但最后请注意半冒号。



然后你有 s 哪个是[some SQL] union s1; union s2;



你看到了吗?



这就是SQL看到的:



You can step through the code and check the value of s before it executes.

Building and Debugging (Visual C#)[^]

This is VERY useful for debugging.

Ok. So I'll give you a hint.

There are two way to end a statement in T-SQl. The keyword GO and the semi colon ;. Their use is slightly different, but that's not important here.

So you have s1 and s2 which are in the syntax "[some SQL];". I'm sure you tested these but note the semi colon on the end.

then you have s which is "[some SQL] union s1; union s2;"

Do you see it?

This is what SQL sees:

[START QUERY] 
[some SQL] union [some SQL]
[END QUERY] <= the semi colon
[START QUERY]
union s2;  
[SYNTAX ERROR NEAR UNION]





当你一次运行一个查询时,你甚至不需要半结肠。如果没有什么可做,声明将终止。如果你想执行更多的动作,你只会在查询中添加它。



只需删除半冒号



When you run a single query at a time, you don't even need a semi colon. The statement will terminate if there is nothing left to do. You would only add it in a query if you wanted to perform more that one action.

Just remove the semi colon


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

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