For using DataSet to create the in-memory structure of data retrieved from the SQL Server Database you can use SqlCommand and SqlDataAdapter objects. You need a SqlConnection object also to initialize and open the database connection to allow the execution of sql command. SqlCommand object accepts the parameters as Sql Query or stored procedure name you have created at back end in sql server programmability section.
SqlDataAdapter object takes SqlCommand and SqlConnection object and enables you to perform the data access without explicitly open or close the sql data connection. SqlDataAdapter Fill method performs automatically, it opens the database connection fills the DataSet passed and closed the database connection. If Sql Data connection is in open state before calling the Fill method then SqlDataAdapter leaves it open after performing the task.
// string variable to store the connection string // defined in appsettings section of web.config file. string connStr = ConfigurationManager.AppSettings[ "ConnectionString" ].ToString();
// object created for SqlConnection Class. SqlConnection mySQLconnection = new SqlConnection(connStr);
SqlCommand mySqlCommand = new SqlCommand("SQL QUERY OR STORED PROC GOES HERE", mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
You can use the DataSet Filled by using SqlDataAdapter when your requirements are as follows:
Where and How to use DataReader?
Be the first to rate this post
Tags: asp.net, ado.net, asp.net performance, ado.net performance, dataset, data reader, sqldatareader, data access
7/24/2008 5:28:21 PM