Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Get data by data type using SqlDataReader

using System; using System.Data; using System.Data.SqlClient; class MainClass {   [STAThread]   static void Main(string[] args)   {     string SQL = "SELECT * FROM Employee";     string ConnectionString ="server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";     SqlConnection conn = new SqlConnection(ConnectionString);     SqlCommand cmd = new SqlCommand(SQL, conn);     conn.Open();     SqlDataReader reader = cmd.ExecuteReader();     try      {       while (reader.Read())        {         Console.Write("ID:"+reader.GetInt32(0).ToString() );         Console.Write(" ,");         Console.WriteLine("Name:" + reader.GetString(1).ToString() );       }     }     finally      {       reader.Close();       conn.Close();     }      } } ID:2 ,Name:G