Create DataViews from DataTable : DataView : ADO.Net C# Examples


C# Examples » ADO.Net » DataView »

 

Create DataViews from DataTable









    
using  System;
using  System.Data;
using  System.Data.SqlClient;

class  MainClass  
{
      static  void  Main(string[]  args)
      {
            string  connString  =  @"server  =  .\sqlexpress;integrated  security  =  true;database  =  northwind";
            string  sql  =  @"select  contactname,  country  from  customers";
            SqlConnection  conn  =  new  SqlConnection(connString);
            try
            {
                  SqlDataAdapter  da  =  new  SqlDataAdapter();
                  da.SelectCommand  =  new  SqlCommand(sql,  conn);

                  DataSet  ds  =  new  DataSet();
                  da.Fill(ds,  "customers");
                  DataTable  dt  =  ds.Tables["customers"];
                  DataView  dv  =  new  DataView(dt,"country  =  'Germany'","country",DataViewRowState.CurrentRows);  
                  foreach  (DataRowView  drv  in  dv)
                  {
                        for  (int  i  =  0;  i  <  dv.Table.Columns.Count;  i++)
                              Console.WriteLine(drv[i]);
                  }
            }
            catch(Exception  e)
            {
                  Console.WriteLine("Error:  "  +  e);
            }
            finally
            {
                  conn.Close();
            }
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» DataView