Microsoft Knowledge Base Email Alertz

KBAlertz.com: (325815) - You can use the SqlCeDataReader class to read a forward-only stream of data rows from a data source. To use the SqlCeDataReader class, you must include a reference to the System.Data.SqlServerCe namespace. The SQlCeDataReader class is an alternative...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 325815 - Last Review: March 28, 2008 - Revision: 6.4

Using SqlCeDataReader with SQL Server CE 2.0, SQL Server 2005 Compact Edition, or SQL Server 2005 Mobile Edition in Visual Basic .NET

This article was previously published under Q325815

On This Page

SUMMARY

In Microsoft SQL Server 2000 Windows CE Edition 2.0, in SQL Server 2005 Compact Edition, or in SQL Server 2005 Mobile Edition, you can use the SqlCeDataReader class to read a forward-only stream of data rows from a data source. To use the SqlCeDataReader class, you must include a reference to the System.Data.SqlServerCe namespace. The SQlCeDataReader class is an alternative of the DataSet. The DataSet is an in-memory cache of data. While the SqlCeDataReader class is in use, the associated SqlCeConnection class is busy serving the SqlCeDataReader class. You cannot perform another operation on the SqlCeConnection class until the SqlCeDataReader class is closed.

MORE INFORMATION

Create a SqlCeDataReader

To create a stream of data rows by using the SqlCeDataReader class, you must call the ExecuteReader method of the SqlCeCommand object to create a SqlCeDataReader class. The following sample code illustrates how to create a SqlCeDataReader class:
        Dim con As SqlServerCe.SqlCeConnection = New SqlServerCe.SqlCeConnection("provider=microsoft.sqlserver.oledb.ce.2.0;data source=\my documents\northwind.sdf")
        con.Open()
        Dim cmd As SqlServerCe.SqlCeCommand
        cmd = New SqlServerCe.SqlCeCommand("Select ProductID from products", con)
        Dim rdr As SqlServerCe.SqlCeDataReader
        rdr = cmd.ExecuteReader()
				
Note This sample code assumes that the My Documents\Northwind.sdf database file already exists on the SQL Server CE device.

Read the Data

After you create the SqlCeDataReader class, you can use it to read the data. To read the stream of data rows by using the SqlCeDataReader class, you must use the Read method of the SqlCeDataReader class. The Read method moves the SqlCeDataReader class to the next record. The Read method also has a Boolean return value. If the return value is TRUE, there are additional rows; if the return value is FALSE, there are no additional rows. For example, you can use the following sample code to read the data:
        While rdr.Read
            ListBox1.Items.Add(rdr(0))
        End While
				

Use the Seek Method

The SqlCeDataReader class has a Seek method that you can use to quickly find rows in a result set (or cursor). When you use the Seek method, you can specify the index and the way that the rows are selected. You must specify the IndexName parameter on the SqlCeCommand object.

By using the SetRange method of the SqlCeCommand object, you can specify the range of the index where you want to search. For additional information about how to set the index range, see the "Advanced Programming Using System.Data.SqlServerCe" topic in SQL Server CE Books Online. The following sample code illustrates the Seek method syntax:
     cmd.commandtype = commandtype.tabledirect
     cmd.commandtext = "products"
     cmd.indexname = "pid"
     rdr = cmd.executereader
     rdr.seek(data.sqlserverce.dbseekoption.before, 5)
				
Note The code assumes that there is a Product ID (PID) index on the ProductID column of the products table.

Close the SqlCeDataReader

After you read the data, you can close the SqlCeDataReader class. Remember that if the SqlCeDataReader class is in use, you cannot perform another operation on the SqlCeConnection class until the SqlCeDataReader class is closed. To check to see if the SqlCeDataReader class is open or closed, call the IsClosed property. The property returns as TRUE if the SqlCeDataReader class is closed and returns as FALSE if it is open. The following code is an example of the IsClosed property:
        If rdr.IsClosed = False Then
             rdr.Close()
        End If
				

REFERENCES

For additional information about the SqlCeDataReader class, see the "SqlCeDataReader Class" topic in SQL Server CE 2.0 Books Online, in SQL Server 2005 Compact Edition Books Online, or in SQL Server 2005 Mobile Edition Books Online.

APPLIES TO
  • Microsoft SQL Server 2000 Windows CE Edition 2.0
  • Microsoft SQL Server 2005 Compact Edition
Keywords: 
kbinfo KB325815
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please