Friday, February 9, 2007

Data Reader

The following sample code is called a DataReader. A foward-only, fast and efficient means of retrieving data.

<%@ Page language="c#" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Configuration" %>

<SCRIPT language=C# runat="server">

void Page_Load(Object sender, EventArgs e)

{



SqlConnection myConnection;

SqlCommand myCommand;

SqlDataReader myDataReader;



myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);

myConnection.Open();


//prepare sql statements

myCommand = new SqlCommand("SELECT TOP 10 * FROM EMPLOYEE", myConnection);

myDataReader = myCommand.ExecuteReader();





while (myDataReader.Read())

{



Response.Write(myDataReader["fname"]);



//Spacing

Response.Write(" ");



Response.Write(myDataReader["minit"]);



//Spacing

Response.Write(" ");



Response.Write(myDataReader["lname"]);



//New Line

Response.Write("<br>");



}



//cleanup objects

myDataReader.Close();

myConnection.Close();





}




</SCRIPT>




No comments: