C# Driver 1.0 for Apache Cassandra

Using bound statements

The previous tutorial used simple CQL statements to read and write data, but you can also use prepared statements, which only need to be parsed once by the cluster, and then bind values to the variables and execute the bound statement you read or write data to a cluster.

In the previous tutorial, you added a LoadData method which creates a new statement for each INSERT, but you may also use prepared statements and bind new values to the columns each time before execution. Doing this increases performance, especially for repeated queries. You add code to your client for:

  • creating a prepared statement
  • creating a bound statement from the prepared statement and binding values to its variables
  • executing the bound statement to insert data

Procedure

  1. Add a new method, LoadData, and implement it.
    public override void LoadData() { }
  2. Add code to prepare an INSERT statement.
    You get a prepared statement by calling the prepare method on your session.
    PreparedStatement statement = Session.Prepare(
          "INSERT INTO simplex.songs " +
          "(id, title, album, artist, tags) " +
          "VALUES (?, ?, ?, ?, ?);");
    
  3. Add code to bind values to the prepared statement's variables and execute it.
    You create a bound statement by calling its constructor and passing in the prepared statement. Use the bind method to bind values and execute the bound statement on the your session..
    BoundStatement boundStatement = new BoundStatement(statement);
    HashSet<String> tags = new HashSet<String>();
    tags.Add("jazz");
    tags.Add("2013");
    Session.Execute(boundStatement.bind(
          new Guid("756716f7-2e54-4715-9f00-91dcbea6cf50"),
          "La Petite Tonkinoise'",
          "Bye Bye Blackbird'",
          "Joséphine Baker",
          tags ) );
    
  4. Add code for the other two insertions into the simplex.songs table.
  5. Add code to create a new bound statement for inserting data into the simplex.playlists table.
    public void LoadData()
    {
       PreparedStatement statement = Session.Prepare(
             "INSERT INTO simplex.songs " +
             "(id, title, album, artist, tags) " +
             "VALUES (?, ?, ?, ?, ?);");
       BoundStatement boundStatement = new BoundStatement(statement);
       HashSet<String> tags = new HashSet<String>();
       tags.Add("jazz");
       tags.Add("2013");
       Session.Execute(boundStatement.bind(
             new Guid("756716f7-2e54-4715-9f00-91dcbea6cf50"),
             "La Petite Tonkinoise'",
             "Bye Bye Blackbird'",
             "Joséphine Baker",
             tags ) );
       statement = Session.Prepare(
             "INSERT INTO simplex.playlists " +
             "(id, song_id, title, album, artist) " +
             "VALUES (?, ?, ?, ?, ?);");
       boundStatement = new BoundStatement(statement);
       Session.Execute(boundStatement.bind(
             new Guid("2cc9ccb7-6221-4ccb-8387-f22b6a1b354d"),
             new Guid("756716f7-2e54-4715-9f00-91dcbea6cf50"),
             "La Petite Tonkinoise",
             "Bye Bye Blackbird",
             "Joséphine Baker") );
    }
    
  6. Replace the call in the class main method to loadData to loadDataUsingBoundStatements.
    static void main(String[] args)
    {
       BoundStatementsClient client = new BoundStatementsClient();
       client.Connect("127.0.0.1");
       client.CreateSchema();
       client.LoadData();
       client.QuerySchema();
       client.UpdateSchema();
       client.DropSchema();
       client.Close();
    }
    
Show/hide