[英]OrderBy clause in C#
This is a method that gets data from a view in my database. 这是一种从数据库视图中获取数据的方法。
public static DataTable GetUnloadingPointList(string whereClause, string connectionString)
{
string strSql = @"SELECT * FROM view_ODW_SI_UnloadingPoints ";
if (!string.IsNullOrEmpty(whereClause))
{
strSql += "where " + whereClause;
}
What I am trying to do here is to add an order by
clause by id. 我在这里尝试做的是通过id添加一个order by
子句。 My export in excel is done from another method, but I need this one. 我在excel中的导出是通过另一种方法完成的,但是我需要这种方法。
Try this one : 试试这个:
public static DataTable GetUnloadingPointList(string whereClause, string connectionString)
{
string strSql = @"SELECT * FROM view_ODW_SI_UnloadingPoints where 1=1 ";
if (!string.IsNullOrEmpty(whereClause))
{
strSql += " and " + whereClause;
}
strSql += " order by id " ;
When you have to insert something in the middle of the string ( where clause or something), I suggest using formatting : 当您必须在字符串中间 ( where子句或其他内容)插入某些内容时,建议使用formatting :
public static DataTable GetUnloadingPointList(string whereClause,
string connectionString) {
String strSql = String.Format(
@" SELECT *
FROM view_ODW_SI_UnloadingPoints
{0}
ORDER BY id",
string.IsNullOrEmpty(whereClause) ? "" : "WHERE " + whereClause);
...
}
formatting is very helpful when you have to assembly a complex string (eg with having , order by , group by etc.). 当您必须组装复杂的字符串时(例如, 具有 ,, 排序依据 , 分组依据等),格式化非常有用。 In C# 6.0 you can use string interpolation : 在C#6.0中,您可以使用字符串插值 :
public static DataTable GetUnloadingPointList(string whereClause,
string connectionString) {
String strSql =
$@" SELECT *
FROM view_ODW_SI_UnloadingPoints
{(String.IsNullOrEmpty(whereClause) ? "" : "WHERE " + whereClause)}
ORDER BY id";
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.