ADO.NET provider for OData / Querying Data
Querying Data

The ADO.NET Provider for OData implements two classes you can use to perform CRUD (Create, Read, Update, and Delete) operations: DbDataReader and DbDataAdapter classes. The tabs below describe the interfaces and code implementations.

Querying With DbDataReader

The DbDataReader class can be used to fetch data in subset increments as required. It can retrieve data quicker than the DbDataAdapter as it retrieves data in pages. When you read data from the DbDataReader, it requests the succeeding page from the data source to load the result, which makes the data retrieval faster.

The following code examples demonstrate create, read, update, and delete operations from the data source using the DbDataReader.

 

Create Data

The example adds new records by executing the Insert command.

C#
Copy Code
using (var conn = new C1ODataConnection(ODataConnectionString))
{
    conn.Open();

    // Create Insert command
    var command = new C1ODataCommand(conn,
        "INSERT INTO books (ISBN, Title, Author, Price, Location_City, Location_Street)
        VALUES ('306-0-355-48016-0', 'Php Expert', 'Charles', 500, 'Delhi', 'MT')");

    // Execute Insert command
    int i = command.ExecuteNonQuery();
    if (i != -1)
    {
        Console.WriteLine("Insert operation successful! \n\n");
    }
}

Read Data

This example retrieves data by executing the Select command.

C#
Copy Code
using (var con = new C1ODataConnection(ODataConnectionString))
{
    con.Open();

    // Create Read command
    var cmd = con.CreateCommand();
    cmd.CommandText = "SELECT * FROM Books LIMIT 10";

    // Execute Read command and display fetched data
    DbDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        Console.WriteLine(String.Format("\t{0} --> \t\t{1}", rdr["Id"], rdr["Title"]));
    }
    Console.WriteLine("Read operation successful! \n\n");
}

Update Data

This example modifies data by executing the Update command.

C#
Copy Code
using (var conn = new C1ODataConnection(ODataConnectionString))
{
    conn.Open();

    // Create Update command
    var command = new C1ODataCommand(conn, "UPDATE Books SET Title = @Title WHERE Id = @Id");
    command.Parameters.AddWithValue("@Id", 4);
    command.Parameters.AddWithValue("@Title", "Patience");

    // Execute Update command
    int i = command.ExecuteNonQuery();
    if (i != -1)
    {
        Console.WriteLine("Update operation successful! \n\n");
    }
}

Delete Data

This example removes data by executing the Delete command.

C#
Copy Code
using (var conn = new C1ODataConnection(ODataConnectionString))
{
    conn.Open();

    // Create Delete command
    var command = new C1ODataCommand(conn, "DELETE FROM books WHERE ID = @ID");
    command.Parameters.AddWithValue("@ID", 5);

    // Execute Delete command
    int i = command.ExecuteNonQuery();
    if (i != -1)
    {
        Console.WriteLine("Delete operation successful! \n\n");
    }
}