using System;

using System.Data;

using System.Data.SqlClient;

using System.Windows.Forms;



namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            string connetionString = null;

            SqlConnection connection ;

            SqlCommand command ;

            SqlDataAdapter adapter = new SqlDataAdapter();

            DataSet ds = new DataSet();

            int i = 0;

            string sql = null;



            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";

            sql = "Your SQL Statement Here";



            connection = new SqlConnection(connetionString);



            try

            {

                connection.Open();

                command = new SqlCommand(sql, connection);

                adapter.SelectCommand = command;

                adapter.Fill(ds);

                adapter.Dispose();

                command.Dispose();

                connection.Close();



                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

                {

                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);



                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("Can not open connection ! ");

            }

        }

    }

}