[英]Having trouble with a database query using SQL Server Management Studio
I'm quite stuck in my programming when I query some of my data, 查询某些数据时,我非常困在编程中,
I want to sort my database student to arrange by year I did use order by year in SQL Server like this: 我想按年份对数据库学生进行排序,以便在SQL Server中按年使用订单,如下所示:
select *
from Student
where year >=1 and year <= 4
order by year;
The code seems to work but when I query my data in the aspx.cs code behind, the program alerts me with an error. 该代码似乎可以正常工作,但是当我在后面的aspx.cs代码中查询数据时,程序会提示我错误。
This is my aspx code behind sample: 这是我的示例后面的aspx代码:
string yearfrom = (this.YearFrom.Value);
string yearto = (this.YearTo.Value);
if(yearfrom != "yearfrom1" && yearto != "yearto2")
{
query = "select * from student where year >= '" + yearfrom + "' and year <= '" + yearto + "order by year'";
}
The query I used in here doesn't seem to work, I don't know why. 我在这里使用的查询似乎不起作用,我也不知道为什么。
I've found the solution the answer post by @Steve is also correct. 我已经找到了@Steve的答案的解决方案也是正确的。
if(yearfrom != "yearfrom1" && yearto != "yearto2")
{
query = @"select * from student
where [year] >= @from and [year] <= @to
order by [year]";
}
this one is also applicable posted by @DeepakPawar a minute ago. 一分钟前由@DeepakPawar发布也适用。
query = "select * from student where year >= " + yearfrom + " and year <= " + yearto + " order by year";
thank you guys for helping me. 谢谢你们对我的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.