﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;
using System.Data.SqlClient;

namespace ConnectAndQuery
{
    class Program
    {
        //Get the settings
        static NameValueCollection settings = ConfigurationManager.AppSettings;

        static void Main(string[] args)
        {
            try
            {
                string connectionString = settings.Get("SQLConnectionString");
                using (SqlConnection connection = new SqlConnection(connectionString)) 
                {
                    //Create a command object
                    SqlCommand cmd = new SqlCommand("Select top 10 CustomerID, FirstName, LastName from SalesLT.Customer",
                        connection);

                    connection.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Write(String.Format("id : {0}, Name : {1} {2}", reader[0], reader[1], reader[2]));
                    }
                }
                Write("Done");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Write(ex.Message);
            }
        }

        private static void Write(string message)
        {
            Console.WriteLine(message);
        }

        private static string GetInput(string message)
        {
            Console.Write(message + " : ");
            return (Console.ReadLine());
        }
    }
}
