Search code examples
cassandracassandra-2.0datastaxdatastax-enterprisecassandra-cli

cassandra c# datastax client insert blob


hi I wonder if there is a more elegant way of inserting blob instead of converting the byte array into string like:

string strblob = "0x00" + BitConverter.ToString(stream2.GetBuffer()).Replace("-", "");

ISession session = cluster.Connect("abc");

session.Execute(string.Format("insert into events(blob) values ({0})", blobStr));

Solution

  • There is a more elegant and more effective solution which is to use bound statement.

    byte[] blob = stream2.GetBuffer();
    ISession session = cluster.Connect("abc");
    
    PreparedStatement preparedStatement = session.Prepare("INSERT INTO events(blob) VALUES (?)");
    BoundStatement boundStatement = preparedStatement.Bind(blob);
    
    session.Execute(boundStatement);
    

    The idea behind PreparedStatement is to parsed the string query once and reuse it as much as you need with different values. So keep track of your prepared statements.