我继承了另一个用vb.net编写的开发人员的应用程序(我还不太流利)。它包含一个数据绑定的GridView,它也会动态生成列的标签。我需要将学年标题更改为例如2009-2010而不是简单的2010.当前代码设置标题的学年,但它似乎也控制显示的数据。
以下是代码:
sql_string_Total = sql_string_Total & " sum([" & Academic_year - 4 & "]) as [" & Academic_year - 4 & "],sum([" & Academic_year - 3 & "]) as [" & Academic_year - 3 & "],sum([" & Academic_year - 2 & "]) as [" & Academic_year - 2 & "],sum([" & Academic_year - 1 & "]) as [" & Academic_year - 1 & "],sum([" & Academic_year & "]) as [" & Academic_year & "], "
sql_string_Total = sql_string_Total & " sum([" & Academic_year & "]-[" & Academic_year - 1 & "]) as [" & (Academic_year - 1) & "-" & (Academic_year) & " Diff.], "
sql_string_Total = sql_string_Total & " (cast(((sum([" & Academic_year & "])-sum([" & Academic_year - 1 & "]))/(sum([" & Academic_year - 1 & "])*1.0)*100.0)as numeric(4,1))) as [" & (Academic_year - 1) & "-" & (Academic_year) & " % Change] "
如何在不影响显示数据的情况下更改这些cols的标题?
答案 0 :(得分:1)
这似乎是SQL-VB问题的混合而不仅仅是VB问题。 SQL中的as
关键字表示字段在结果集中的名称。说完了......
快速解决方案:只需根据需要更改字段名称即可。例如,更改:
sql_string_Total = sql_string_Total & " sum([" & Academic_year - 4 & "]) as [" & Academic_year - 4 & "],sum([" & Academic_year - 3 & "]) as [" & Academic_year - 3 & "],sum([" & Academic_year - 2 & "]) as [" & Academic_year - 2 & "],sum([" & Academic_year - 1 & "]) as [" & Academic_year - 1 & "],sum([" & Academic_year & "]) as [" & Academic_year & "], "
到
sql_string_Total = sql_string_Total & " sum([" & Academic_year - 4 & "]) as [" & Academic_year - 5 & "-" & Academic_year - 4 & "],sum([" & Academic_year - 3 & "]) as [" & Academic_year - 4 & "-" & Academic_year - 3 & "],sum([" & Academic_year - 2 & "]) as [" & Academic_year - 3 & "-" & Academic_year - 2 & "],sum([" & Academic_year - 1 & "]) as [" & Academic_year - 2 & "-" & Academic_year - 1 & "],sum([" & Academic_year & "]) as [" & Academic_year - 1 & "-" & Academic_year & "], "
前一个开发人员对sum to sum和sum将具有的名称使用相同的变量,但是您可以将名称更改为您需要的名称,而不会影响将要查询的数据。在gridview中,该列将采用您为结果列指定的名称。
缓慢,推荐的解决方案:重写那段代码。我知道这不是你的,但正如你所看到的,它是一个小怪物,任何编辑都会让它变得更糟;更不用说创建dinamyc sql是一种非常糟糕的方法。寻找一种重写它的方法也可以帮助你提高你的VB.net技能,我觉得如果你想要暂时搁置这个项目,你将需要它们。我建议您先查看String.Format方法,然后Parameterized Queries开始。