Wednesday, March 28, 2012

is this the best method to connect to a db

is this the best and fastest method to connect to a database for DataGrid databind?

---
Dim MyConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringSQL"))
Dim MyCommand As SqlCommand = New SqlCommand("sp_BuddiesPendingSelect1", MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure
MyCommand.Parameters.Add(New SqlParameter("@.UserID", intUserID))

MyConnection.Open()
Dim dr As SqlDataReader = MyCommand.ExecuteReader()

DataList1.DataSource = dr
DataList1.DataBind()

dr.Close()
MyConnection.Close()

--------
or should i use a DataAdapter and fill a DataSet and then attach the DataSet to the DataGrid?Your code should be the fastest way. Using a DataAdapter/DataSet itself calls a DataReader to fill the DataSet. So if you used the DataAdapter/DataSet you'd still get the DataReader plus the overhead of filling the DataSet and then giving that to the DataGrid.

But if you want to be able to manipulate the data on postbacks without having to re-query, then using a DataSet is the way to go.|||thanks for the quick response. does DataSet work on application level, session level or page level?
will person A have access to a DataSet filled by peson B with different session?|||You decide that. If the DataSet is just for a user interacting with a page then persist it across postbacks in a Session variable or in the page's ViewState. Only use ViewState if the DataSet isn't large as it will slow down performance. If you use a Session variable be sure to clear out the DataSet once it's no longer needed. This will conserver server resources.

If it's to be shared among many users and/or many pages then Cache would be a good place to persist it. Every user can access it there.

No comments:

Post a Comment