Visual Basic World

Just another Visual Basic weblog

Visual Studio 2005 Cookbook – Database, Storing the Results of a Query in Memory

Recipe 13.7. Storing the Results of a Query in Memory

Problem

While a data reader is fast and convenient, you would like to keep the retrieved data around for a while, even when you are disconnected from the database or other data source.

Solution

Use the data reader to bring the results into a data set. The DataSet object represents one or more in-memory database tables, each with its records stored in a separate DataTable object.

Discussion

The following code loads all records from the Table1 table into a DataSet object, creating a DataTable object named Table1 within that data set:

	' ----- Connect to the database.
	Dim 
connectionString As String = _
	   “Data Source=MySystemSQLEXPRESS;” & _
	   “Initial Catalog=MyDatabase;Integrated Security=true”
	Dim theDatabase As New SqlClient.SqlConnection(connectionString)
	theDatabase.Open()

	‘ —– Prepare the SQL statement for use by the data set.
	Dim sqlStatement As New SqlClient.SqlCommand( _
	   “SELECT * FROM Table1″, theDatabase)
        Read More >>

July 9, 2008 - Posted by | database, programing, vb.net | , , , , , , , , , ,

No comments yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.