title: How to run a database script file from Delphi?
body: <p>I want to do the following.
  1)  Create a database.
  2)  Run a script when creates tables, stored procedures, etc.  (this script is created by SMS 'generate scripts' option)</p>
  
  <p>I found the following code:  <a href="http://www.delphipages.com/forum/showthread.php?t=181685" rel="nofollow noreferrer">http://www.delphipages.com/forum/showthread.php?t=181685</a>
  and modified it to this:</p>
  
  <blockquote>
    <p>try</p>
  
  <pre><code>ADOQuery.ConnectionString := 'Provider=SQLOLEDB.1;Password=' +
  </code></pre>
    
    <p>edtPassword.Text
            + ';Persist Security Info=True;User ID=' + edtUser.Text
            + ';Initial Catalog=master;Data Source=' +
    edtServerName.Text;</p>
  
  <pre><code>ADOQuery.SQL.Clear;
  ADOQuery.SQL.Text := 'create DataBase ' + edtWebDBName.Text;
  ADOQuery.ExecSQL; // should check existance of database
  ADOWeb.Connected := false;
  ADOWeb.ConnectionString := 'Provider=SQLOLEDB.1;Password=' +
  </code></pre>
    
    <p>edtPassword.Text
              + ';Persist Security Info=True;User ID=' + edtUser.Text
              + ';Initial Catalog=' + edtWebDBName.Text + ';Data Source=' +
    edtServerName.Text;
        ADOWeb.Connected := true;</p>
  
  <pre><code>ADOQuery.Connection := ADOWeb;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.LoadFromFile(edtScriptFileName.Text);
  ADOQuery.ExecSQL;   except
  </code></pre>
  </blockquote>
  
  <p>This works up until the point of running the script file.  Then it generates an exception: Incorrect Syntax near "GO".  If i run the script in SMS on the newly created DB, it is fine.  Is this issue due to running more than one SQL command at once (the script is essentially a long list of command/GO statements?  How to get around it?   </p>
  
  <p>Oh also as a bonus, any thoughts on a quick check to see if the new database actually exists before sending a script to it?  (Or is it not necessary since if the create fails it will generate exception?)</p>