【问题标题】:Order string column is not working properly订单字符串列无法正常工作
【发布时间】:2018-06-22 16:34:47
【问题描述】:

我有一个名为 sang.mdf 的数据库,在 C# Windows 窗体中,序列号列的升序无法正常工作。

我试过了:

SqlDataAdapter sda = new SqlDataAdapter("select slno, date, name, total from printi ORDER BY slno ASC", con);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();

使用此代码,排序后的元素如下所示:

slno   date     name     total

1    
10  
11  
12  
13  
2  
20  
21  
3  
4    
5  

【问题讨论】:

  • 工作不正常?它表现如何?列是 varchar 类型吗?如果数据类型是 varchar
  • 序号顺序为1、10、11、12、13、14、2、20、21、22、3、4、5
  • SELECT *, CAST(yourColumn AS int) AS yourColumnInt FROM yourTable ORDER BY yourColumnInt 试试看
  • coloum slno 是 varchar 格式。我将其更改为 Int。所以排序工作......谢谢
  • 您也可以尝试 select col1, col2, ... from yourTable order by case when isumeric(Field1) = 1 then cast(col1 as int) else null end Yes slno 是 varchar 格式,因此你必须将其转换为 INT 才能执行排序(排序为整数)

标签: c# sql sql-server sorting


【解决方案1】:

您需要将您的列值cast 转换为 int。像这样:

cast(slno as int)

在 C# 中:

SqlDataAdapter sda = new SqlDataAdapter("select slno, date, name, total " +
                                        "from printi ORDER BY cast(slno as int) ASC", con);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-22
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多