Wednesday, June 10, 2015

C# ADO.NET SqlDataReader

SqlDataReader Object provides a connection oriented data access to the SQL Server data Sources from C# applications. ExecuteReader() in the SqlCommand Object sends the SQL statements to the SqlConnection Object and populate a SqlDataReader Object based on the SQL statement or Stored Procedures.
 
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
  
When the ExecuteReader method in the SqlCommand Object execute , it will instantiate a SqlClient.SqlDataReader Object. When we started to read from a DataReader it should always be open and positioned prior to the first record. The Read() method in the DataReader is used to read the rows from DataReader and it always moves forward to a new valid row, if any row exist .
 
SqlDataReader.Read();
  
using System;
using System.Windows.Forms;
using System.Data.SqlClient; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection sqlCnn ;
            SqlCommand sqlCmd ;
            string sql = null;

            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            sql = "Your SQL Statement Here , like Select * from product";

            sqlCnn = new SqlConnection(connetionString);
            try
            {
                sqlCnn.Open();
                sqlCmd = new SqlCommand(sql, sqlCnn);
                SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                while (sqlReader.Read())
                {
                    MessageBox.Show(sqlReader.GetValue(0) + " - " + sqlReader.GetValue(1) + " - " + sqlReader.GetValue(2));
                }
                sqlReader.Close();
                sqlCmd.Dispose();
                sqlCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • SQL FULL JOIN SQL FULL JOIN Keyword The FULL JOIN keyword return rows when there is a match in one of the tables. SQL FULL JOIN Syntax SELECT column_name(s) FR… Read More
  • sample code for call webservice using ajax in asp.net Here we call webservice using ajax update panal and javascript . We see also how an ASP.NET AJAX-enabled web service generates the JavaScript needed … Read More
  • SQL INNER JOIN Keyword SQL INNER JOIN Keyword The INNER JOIN keyword returns rows when there is at least one match in both tables. SQL INNER JOIN Syntax SELECT column_n… Read More
  • Anonymous Methods in asp.net In versions of C# previous to 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduces anonymous methods c#. Creating anon… Read More
  • SQL RIGHT JOIN SQL RIGHT JOIN Keyword The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no matches in the left tab… Read More

0 comments:

Post a Comment