Querying with the DataReader
The CData ADO.NET Provider for MongoDB implements two ADO.NET interfaces you can use to retrieve data from MongoDB: MongoDBDataAdapter and MongoDBDataReader objects. Whereas MongoDBDataAdapter objects retrieve a single result set of all the data that matches a query, MongoDBDataReader objects fetch data in subset increments as needed.
Using the MongoDBDataReader
The MongoDBDataReader retrieves data faster than the MongoDBDataAdapter because it can retrieve data in pages. As you read data from the MongoDBDataReader, it periodically requests the next page of results from the data source, if required. This causes results to be returned at a faster rate. The following example selects all the columns from the [CData].[Sample].Customers table:
C#
string connectionString = "Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;"; using (MongoDBConnection connection = new MongoDBConnection(connectionString)) { MongoDBCommand cmd = new MongoDBCommand("SELECT * FROM [CData].[Sample].Customers", connection); MongoDBDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(String.Format("\t{0} --> \t\t{1}", rdr["City"], rdr["CompanyName"])); } }
VB.NET
Dim connectionString As String = "Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;" Using connection As New MongoDBConnection(connectionString) Dim cmd As New MongoDBCommand("SELECT * FROM [CData].[Sample].Customers", connection) Dim rdr As MongoDBDataReader = cmd.ExecuteReader() While rdr.Read() Console.WriteLine([String].Format(vbTab & "{0} --> " & vbTab & vbTab & "{1}", rdr("City"), rdr("CompanyName"))) End While End Using