如何在数据集中排序 [英] How to sort in dataset

查看:72
本文介绍了如何在数据集中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PLZ我尝试了很多东西,但是我不能在表单中对我的数据库进行排序我不善于要求我使用此代码来从我想要排序列名称(回答)以获取空调用首先



我尝试了什么:



plz i try many thing but i can't sort my database in form im not good in asking i use this code to my from i want to sort column name ( answer ) to get the null call first

What I have tried:

private void Interior_Load(object sender, EventArgs e)
       {



           string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
           using (SqlConnection sqlconn = new SqlConnection(mainconn))
           {
               sqlconn.Open();

               using (SqlCommand sqlcomm = new SqlCommand("SELECT * FROM remaining WHERE username=@username", sqlconn))
               {
                   sqlcomm.Parameters.AddWithValue("@username", txtusername.Text);
                   using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
                   {

                       ds = new DataSet();
                       sda.Fill(ds);


                   }
               }
       }
     }

推荐答案

最简单的方法?

在SQL上使用ORDER BY子句:

Easiest way?
Use an ORDER BY clause on your SQL:
using (SqlCommand sqlcomm = new SqlCommand("SELECT * FROM remaining WHERE username=@username ORDER BY MySortColumn DESC", sqlconn))





但是......两件事情。

1)不要使用 SELECT * FROM - 始终按照要检索的顺序命名要检索的列。这样,您的代码或显示不会因未来对数据库的更改而搞砸,并且您不会浪费内存或带宽来获取您不会使用的数据。

2)为什么你有多个用户使用相同的用户名吗?通常,用户名唯一地标识用户并且具有相同名称的两个用户会使问题复杂化...



But ... tw things.
1) Don't use SELECT * FROM - always name the columns you want to retrieve in the order you want to retrieve them. That way your code or display doesn't get messed up by a future change to the DB, and you don't waste memory or bandwidth fetching data you aren't going to use.
2) Why would you have more than one user with a same username? Normally, the username uniquely identifies a user and having two with the same name complicates matters...


请参考:排序和过滤数据| Microsoft Docs [ ^ ]


这篇关于如何在数据集中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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