title: How to use USE DBName after CREATE DATABASE DBName in one script in Transact-SQL?
body: <p>I am trying to write a code like this in T-SQL</p>
  <pre><code>BEGIN TRANSACTION
  
  CREATE DATABASE DBName
  
  USE DBName
  
  BEGIN TRY
      --Some sample query that could go wrong
  END TRY
  BEGIN CATCH
      ROLLBACK
  END CATCH
  </code></pre>
  <p>I faced the error</p>
  <blockquote>
  <p>DBName Database doesnt exist</p>
  </blockquote>
  <p>This answer advised to use GO keyword
  <a href="https://stackoverflow.com/questions/14499378/how-create-and-use-database-directly-after-creation-in-sql-server">(Click Here)</a></p>
  <p>I faced the error</p>
  <blockquote>
  <p>Syntax error near 'GO'</p>
  </blockquote>
  <p>This answer tells that GO can't be used in T-SQL
  <a href="https://stackoverflow.com/questions/25680812/incorrect-syntax-near-go">(Click Here)</a></p>
  <p><strong>I am using C# OleDBConnection to execute the SQL code</strong></p>
  <p>It works if I separately execute CREATE DATABASE first and then execute USE DBName in a separate file.</p>
  <p>But in that case ROLLBACK would not rollback the database creation command and I will be left with a blank database. That is why, I need to include both CREATE DATABASE and USE DATABASE commands in the same file</p>
  <p><strong>How can I use something which is equivalent to GO keyword in T-SQL ?</strong></p>