title: How to use SqlCommand to CREATE DATABASE with parameterized db name?
body: <p>To put it short. I've got two simple helpers:</p>
  
  <pre><code>    private SqlCommand CreateCommand(string text)
      {
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = connection;
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = text;
          return cmd;
      }
  
      void SetParameter(SqlCommand cmd, string p, string dbName)
      {
          cmd.Parameters.Add(p, SqlDbType.NVarChar);
          cmd.Parameters[p].Value = dbName;
      }
  </code></pre>
  
  <p>This executes OK:</p>
  
  <pre><code>var cmd = CreateCommand("CREATE DATABASE Demo "+
              @"ON (FILENAME = N'c:\demo_data.mdf') "+ 
              @"LOG ON (FILENAME = N'c:\demo_data.mdf.LDF') "+
              "FOR ATTACH " +
              "GO");
  cmd.ExecuteNonQuery();
  </code></pre>
  
  <p>But this doesn't:</p>
  
  <pre><code>string dataBaseAttachText = "CREATE DATABASE @dbname " +
                                "ON (FILENAME = @filename) " +
                                "LOG ON (FILENAME = @filenamelog) " +
                                "FOR ATTACH GO";
  var cmd = CreateCommand(dataBaseAttachText);
  
  SetParameter(cmd, "@dbname", "Demo");
  SetParameter(cmd, "@filename", @"c:\demo_data.mdf");
  SetParameter(cmd, "@filenamelog", @"c:\demo_data.mdf.LDF");
  
  cmd.ExecuteNonQuery();
  </code></pre>
  
  <p>Why?</p>