Navigator: Home - Retrieve - Retrieve data from SQL Server 2005

Retrieve data from SQL Server 2005

Retrieve data from SQL Server 2005

In this example we're going to load data from the Datawarehouse table. The database we're connecting to is already established in the connection string. The connString is an entry in the Web.Config file and will look like this:

<appSettings>
<add key="connString" value="data source=yourIPaddress;User ID=User; Password=Pass;initial catalog=datahouse"/>
</appSettings>



The method to actually retrieve the data using a data reader will look like this:
public void LoadData()

{
SqlCommand cmd = new SqlCommand("Select * From Datawarehouse", new SqlConnection(ConfigurationManager.AppSettings["connString"]));
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
DataTitle.Text = dr["DataTitle"].ToString();
DataDescription.Content = dr["DataDescription"].ToString();
DataKeywords.Content = dr["DataKeywords"].ToString();
}
else
{

}
cmd.Connection.Close();
cmd.Connection.Dispose();
dr.Close();
}


It's equally important to close and dispose of the connection and connection object.
411asp.net123aspxDotNetFreaksServer Intellect