Monday, April 28, 2008

Bind data with datareader

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("
");

}
//cleanup objects
myDataReader.Close();
myConnection.Close();
}

No comments:

Loading...