.Net > Beginner

What would be the output of following code snippet?

Suppose we have database "Database" and "Tab" table in the database, and have two columns name and age with two rows as below

Tab - Table
----------------------
name | age
----------------------
Manoj | 25
Vishal | 26
----------------------


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

class Program
{
    static string sqlConnectionString = @"Server=.\SQLEXPRESS;Database=DataBase;Trusted_Connection=True;";
    static void Main(string[] args)
    {
        SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
        try
        {
            SqlCommand sqlCommand = new SqlCommand("Select * from Tab", sqlConnection);
            sqlConnection.Open();
            SqlDataReader dataReader = sqlCommand.ExecuteReader();
            if (sqlConnection.State != ConnectionState.Closed)
            {
                sqlConnection.Close();
            }
            while (dataReader.Read())
            {
                Console.WriteLine(dataReader["name"].ToString() + dataReader["age"].ToString());
            }
        }
        catch (Exception ex)
        {
            //Log the exception..send mail..etc
            Console.WriteLine("Exception occurred : " + ex.Message);
        }
        finally
        {
            if (sqlConnection.State != ConnectionState.Closed)
            {
                sqlConnection.Close();
            }
        }
        Console.ReadLine();
    }
}
Answers :
Time Left
Question Number : /20
Please do not press back or refresh button during this test.

Login

Register

Next Question