Execute two sql commands : SqlCommand : ADO.Net C# Examples


C# Examples » ADO.Net » SqlCommand »

 

Execute two sql commands









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

class  MainClass
{
        public  static  void  Main()
        {
                using  (SqlConnection  con  =  new  SqlConnection())
                {
                        con.ConnectionString  =  @"Data  Source  =  .\sqlexpress;Database  =  Northwind;  Integrated  Security=SSPI";

                        using  (SqlCommand  com  =  con.CreateCommand())
                        {
                                com.CommandType  =  CommandType.Text;
                                com.CommandText  =  "SELECT  BirthDate,FirstName,LastName  FROM  "+
                                        "Employees  ORDER  BY  BirthDate;SELECT  *  FROM  Employees";

                                con.Open();

                                using  (SqlDataReader  reader  =  com.ExecuteReader())
                                {
                                        while  (reader.Read())
                                        {
                                                Console.WriteLine("    {0,18:D}  -  {1}  {2}",
                                                        reader.GetDateTime(0),    //  Retrieve  typed  data
                                                        reader["FirstName"],        //  Use  string  index
                                                        reader[2]);                          //  Use  ordinal  index
                                        }
                                        Console.WriteLine(Environment.NewLine);
                                        reader.NextResult();
                                        Console.WriteLine("Employee  Table  Metadata.");
                                        for  (int  field  =  0;  field  <  reader.FieldCount;  field++)
                                        {
                                                Console.WriteLine("    Column  Name:{0}    Type:{1}",
                                                        reader.GetName(field),
                                                        reader.GetDataTypeName(field));
                                        }
                                }
                        }
                }
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlCommand