Calling Stored Procedures
You can invoke a stored procedure using MongoDBCommand in the same way as any other SQL stored procedure. To instantiate a MongoDBCommand object, provide the name of the stored procedure and a MongoDBConnection instance as arguments to the constructor. Set the value of the CommandType property to "StoredProcedure" and add the parameters as key-value pairs to the Parameters collection of the MongoDBCommand instance.
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("AddDocument", connection); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new MongoDBParameter("@Collection", "TestCollection")); // Add other parameters as needed ... MongoDBDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { for (int i = 0; i < rdr.FieldCount; i++) { Console.WriteLine(rdr.GetName(i) + " --> " + rdr[i]); } Console.WriteLine(); } }
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("AddDocument", connection) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add(New MongoDBParameter("@Collection", "TestCollection")) ' Add other parameters as needed ... Dim rdr As MongoDBDataReader = cmd.ExecuteReader() While rdr.Read() For i As Integer = 0 To rdr.FieldCount - 1 Console.WriteLine(rdr.GetName(i) + " --> " + rdr(i)) Next Console.WriteLine() End While End Using
Alternatively, you can set the parameters of a stored procedure in the text of the command. The support for stored procedure statements follows the standard form shown below:
"EXECUTE my_proc @first = 1, @second = 2, @third = 3;" "EXEC my_proc @first = 1, @second = 2, @third = 3;"
To execute a parameterized query, add parameters as key-value pairs to the Parameters collection of the MongoDBCommand instance.
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("EXECUTE AddDocument Collection = @Collection;", connection); cmd.Parameters.Add(new MongoDBParameter("@Collection", "TestCollection")); // Add other parameters as needed ... MongoDBDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { for (int i = 0; i < rdr.FieldCount; i++) { Console.WriteLine(rdr.GetName(i) + " --> " + rdr[i]); } Console.WriteLine(); } }
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("EXECUTE AddDocument Collection = @Collection;", connection) cmd.Parameters.Add(New MongoDBParameter("@Collection", "TestCollection")) ' Add other parameters as needed ... Dim rdr As MongoDBDataReader = cmd.ExecuteReader() While rdr.Read() For i As Integer = 0 To rdr.FieldCount - 1 Console.WriteLine(rdr.GetName(i) + " --> " + rdr(i)) Next Console.WriteLine() End While End Using