简体   繁体   English

使用SQL Server Management Studio进行数据库查询时遇到麻烦

[英]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.

相关问题 在SQL Server Management Studio中找不到数据库 - Unable to find database in SQL Server Management Studio 如何在SQL Server Management Studio中使用Visual Studio创建连接数据库? - How connect database created in SQL Server Management Studio with Visual Studio? 部署在本地SQL Server Management Studio中的Azure数据库的数据库备份 - Database backup of azure database deployed in local sql server management studio 使用C#应用程序远程访问使用SQL Server 2014 Management Studio创建的数据库 - Remote access using C# application to database created using SQL Server 2014 Management Studio 使用SQL Management Studio访问SharePoint Online SQL Server数据库 - Access SharePoint Online SQL Server Database with SQL Management Studio .NET 应用程序中的 SQL 查询速度很慢,但在 SQL Server Management Studio 中是即时的 - SQL Query slow in .NET application but instantaneous in SQL Server Management Studio 将LocalDB附加到Microsoft SQL Server Management Studio会破坏数据库 - Attach LocalDB to microsoft sql server management studio corrupts database 实体框架代码首先在SQL Server Management Studio中没有数据库 - Entity Framework Code First no database in SQL Server Management Studio 当我可以使用具有相同登录名的SQL Server Management Studio进行连接时,为什么我的应用程序不连接到SQL Server数据库? - Why won't my application connect to SQL Server database when I can connect using SQL Server Management Studio with the same login? 在SQL Server Management Studio Express中取得此查询如何使用? - Made this query in SQL Server Management Studio Express how to use it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM