DataSet数据筛选和排序

ContractedBlock.gifCode

//先创建数据适配器,

SqlDataAdapter da=new SqlDataAdapter();

da.SelectCommand=new SqlCommand(sql,conn);

//创建和填充数据集

DataSet ds=new DataSet();

da.Fill(ds,"Customers");

//每个查询返回一个数据集,每个结果都保存在单独的DataTable中.第一个显示地命名Customers,第二个表达默认名词为Customers1.

//从数据集的Tables属性获得DataTables对象集合

DataTableCollection dtc=ds.Tables;

 在显示第一个表时:

string fl="country='Germany'";//sql where

string srt="CompanyName desc";//sql desc

ExpandedBlockStart.gif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            SqlConnection conn 
= new SqlConnection("server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
            
string sql1 = @"select * from customers ";//Notice:add a space after the string sql1
            string sql2 = @"select * from products where unitprice<10";
            
string sql = sql1 + sql2;
            
try
            {
                
                SqlDataAdapter da 
= new SqlDataAdapter();
                da.SelectCommand 
= new SqlCommand(sql, conn);
                DataSet ds 
= new DataSet();

                da.Fill(ds, 
"customers");
                
//Console.WriteLine("I love you !");
                
                
//get the table collections 
                DataTableCollection dtc = ds.Tables;//the tables collections of the ds
                
//display data from the first table
                Console.WriteLine("Result from the customers tables:");
                
                Console.WriteLine(
"CompanyName".PadRight(20),"ContactName".PadRight(23)+"\n");
                
//set display filter
                
                
string fl = "country='Germany'";
                
//set sort
                string srt = "Companyname";
                
//display the filtered and sorted data
                foreach (DataRow row in dtc["customers"].Select(fl, srt))
                {
                    Console.WriteLine(
"{0}\t{1}",row["companyname"].ToString().PadRight(25),row["contactname"].ToString());
                }
                
//display the data from the second table
                Console.WriteLine("\n-----------------------------------------------------");
                Console.WriteLine(
"The result from products :");
                Console.WriteLine(
"ProductName".PadRight(20),"UintPrice".PadRight(21));
                
//display data
                foreach (DataRow row in dtc[1].Rows)
                {
                    Console.WriteLine(
"{0}\t{1}",row["productname"].ToString().PadRight(25),row["unitprice"].ToString());
                }
                
            }
            
catch (SqlException ex)
            {
                Console.WriteLine(
"The error {0}", ex.Message);
            }
            
finally
            {
                conn.Close();
            }
            Console.ReadLine();
        }
    }
}

转载于:https://www.cnblogs.com/binlyzhuo/archive/2008/11/21/1338657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值