Friday, August 10, 2007

Steps in getting data from Database

1. Objects needed to retrieve data from Database: SqlConnection, SqlDataAdapter, DataSet

2. Instantiate SqlConnection object by passing it the ConnectionString
SqlConnection con = new SqlConnection(“connectionString”);

3. Open the connection using Open method of SqlConnection object
con.Open();

4. Instantiate DataSet object
DataSet ds = new DataSet();

5. Instantiate DataAdapter object by passing it the sql query and the connection object
SqlDataAdapter da = new SqlDataAdapter(“sqlQuery here”,con)

6. Call the Fill method of DataAdapter, passing it the DataSet object
da.Fill(ds);

7. Assign the DataSet object as Grid’s DataSource
Grid1.DataSource = ds;

8. Call the DataBind method of DataGrid
Grid1.DataBind();

No comments:

Post a Comment