title: Npgsql: How do Prepared Statements
body: <p>we are developing a database backend for a multiplayer game. The server is written in C# and talks to a Postgres database via Npgsql.</p>
  
  <p>Now, the manual shows how to use prepared statements:</p>
  
  <pre><code>NpgsqlCommand command = new NpgsqlCommand("select * from tablea where column1 = :column1", conn);
  // Now add the parameter to the parameter collection of the command specifying its type.
  command.Parameters.Add(new NpgsqlParameter("column1", NpgsqlDbType.Integer);
  // Now, prepare the statement.
  command.Prepare();
  // Now, add a value to it and later execute the command as usual.
  command.Parameters[0].Value = 4;
  </code></pre>
  
  <p>And it states that prepared statements are valid only inside a database session.
  I have two questions now:</p>
  
  <p>1.) Will the server recognize that this command was prepared if I create a new NpgsqlCommand object with the same command text and parameter types or do I have to keep the object and simply change the variables before executing it again? In the example the command object is disposed after the query.</p>
  
  <p>2.) Will prepared statements probably increase the performance although we ONLY have simple statements in this style:</p>
  
  <pre><code>SELECT f1,f2 FROM t1
  UPDATE t1 SET f1=1, f2=2
  </code></pre>
  
  <p>It is possible that hundreds of queries with the same style are executed in a row - currently creating a new NpgsqlCommand (and also NpgsqlConnection from NpgSql's pool) object for each query.</p>
  
  <p>Thanks!</p>