title: Unable to execute script from c# using smo
body: <p>I have developed a C# Windows application in which I need to create a database on client machine for which I have created a script file and installed SQL Server 2008 R2 on client machine. </p>
  
  <p>But when I execute script from my code it always shows an error: </p>
  
  <blockquote>
    <p>Failed to connect database. </p>
  </blockquote>
  
  <p>I have added referenced the SMO assemblies from </p>
  
  <pre><code>C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll 
  </code></pre>
  
  <p>And my code is :</p>
  
  <pre><code>string sqlConString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mydatabase;Data Source=(local)";
  FileInfo file = new FileInfo(dbPath);
  
  string script = file.OpenText().ReadToEnd();
  
  SqlConnection con = new SqlConnection(sqlConString);
  
  Server server = new Server(new ServerConnection(con));
  server.ConnectionContext.ExecuteNonQuery(script);
  
  file.OpenText().Close();
  </code></pre>