字符串值按升序排列 [英] string value into ascending order

查看:99
本文介绍了字符串值按升序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

使用值按升序排序c#代码。

我的问题是我无法按升序列出值。如果值是字符串。

但我可以按升序列出值。如果值是整数。

我的问题是,有没有办法将字符串值提升到升序?



我的代码是:

数据库:

价格varchar(50)

状态varchar(50)



Hi all,
am stuck up with c# code using value sort out in ascending order.
my problem is i cannot list out the values in ascending order. if the value is string.
but i can list out the values in ascending order. if the value is integer.
my question is, is there any way to bring up string value to ascending order?

my code is:
database:
price varchar(50)
status varchar(50)

c# code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ascending : System.Web.UI.Page
{
    string str=ConfigurationManager.AppSettings["const"].ToString();
    SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        filladapriceband();
       
    }

    private object filladapriceband()
    {
        con = new SqlConnection(str);
        ddpriceband.Items.Clear();
        SqlDataReader dr = default(SqlDataReader);
        SqlCommand sqlcmd = default(SqlCommand);
        int count1 = 0;
        count1 = 0;
        try
        {
            sqlcmd = new SqlCommand("select distinct(price) from priceband where status=0 order by price asc", con);

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

           //dr = sqlcmd.ExecuteReader;
            dr = sqlcmd.ExecuteReader();
            while ((dr.Read()))
            {
                //ddpriceband.Items.Add(dr(0));
                ddpriceband.Items.Add(dr[0].ToString());
           
                count1 = count1 + 1;
            }

            ddpriceband.Items.Add("All");
       
            Session.Add("selpb", "All");
            return 0;

        }
        catch (Exception ex)
        {
            lblStatus.Text = "Unable to dispaly the avaliable pricebands.";
          
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
        return 0;
    }
}





请为此提出解决方案,非常感谢你。



please drop a solution for this, its very much thankful to you.

推荐答案

尝试使用

try with
select distinct(price), CONVERT(INT, price) from priceband where status=0 order by 2 asc







引用:

我收到以下错误,

将varchar值'0.5'转换为数据类型int时转换失败。

am getting the below error,
Conversion failed when converting the varchar value '0.5' to data type int.





表示你​​的列中有小数值,你可以投我漂浮或金钱



CAST(浮动价格) cast(价格为金钱)


修改此:

modify this:
order by price



到,如果是整数:


to, if for integer:

order by convert(int, price)



或者,如果是浮动:


or, if for float:

order by convert(float, price)


嗨朋友,试试这个:

Hi Friend, Try this :
select * from priceband 
where status=0 
order by CASE IsNumeric(price) WHEN 1 THEN Replicate('0', 100 - Len(price)) + price ELSE price     END


这篇关于字符串值按升序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆