title: C# - Create SQL Server table programmatically
body: <p>I am trying to create a SQL Server table programmatically. Here is the code.</p>
  
  <pre><code>using (SqlConnection con = new SqlConnection(conStr))
  {
  
      try
      {
          //
          // Open the SqlConnection.
          //
          con.Open();
          //
          // The following code uses an SqlCommand based on the SqlConnection.
          //
          using (SqlCommand command = new SqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
              command.ExecuteNonQuery();
  
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
  }
  </code></pre>
  
  <p>When I'm running this application second time I'm getting an exception:</p>
  
  <blockquote>
    <p>"There is already an object named 'Customer' in the database" </p>
  </blockquote>
  
  <p>but when I check database I don't see such a table.<br>
  Here is my connection string.</p>
  
  <pre><code>&lt;connectionStrings&gt;
    &lt;add name ="AutoRepairSqlProvider" connectionString=
       "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AutoRepairDatabase.mdf;
       Integrated Security=True;User Instance=True"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>When I am running select query; I am getting results from existing tables so I think connection string should be OK. Hope you'll see the problem :/</p>