Create DataView through DataTable : DataView : Database C# Source Code


Custom Search

C# Source Code » Database » DataView »

 

Create DataView through DataTable








    

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

   class DataViewExample
   {
      static void Main()
      {
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string sql = @"select * from employee";
         SqlConnection conn = new SqlConnection(connString);

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

            DataSet ds = new DataSet();
            da.Fill(ds, "employee");

            DataTable dt = ds.Tables["employee"];

            DataView dv = new DataView(dt,"lastname = 'Z'", "lastname", DataViewRowState.CurrentRows);

            foreach (DataRowView drv in dv)
            {
               for (int i = 0; i < dv.Table.Columns.Count; i++){
                  Console.Write(drv[i] + "\t");
               }   
            }
         }
         catch(Exception e)
         {
            Console.WriteLine("Error: " + e);
         }
         finally
         {
            conn.Close();
         }
      }
   }


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» DataView