title: Microsoft SQL Server CE Check if table exists
body: <p>I am trying to check if a table exists in Microsoft SQL Server, but somehow the function I use always returns that it doesn't exist, and the output doesn't specify that an exception was thrown.</p>
  
  <p>This occurs both before I created the table (which is expected), as well as after I created the table (which is not expected).</p>
  
  <p>This is the function I am using:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Checks  if a certain Sql Server Table exists
  /// &lt;/summary&gt;
  /// &lt;param name="_databasename"&gt;The name of the database&lt;/param&gt;
  /// &lt;param name="_password"&gt;The password of the database&lt;/param&gt;
  /// &lt;param name="_tablename"&gt;The name of the table to check&lt;/param&gt;
  /// &lt;returns&gt;
  ///     'true' if table exists
  ///     'false' if table not exists or if an exception was thrown
  /// &lt;/returns&gt;
  public Boolean TableExists(String _databasename, String _password, String _tablename)
  {
      if (!_databasename.Contains(".sdf")) { _databasename = _databasename + ".sdf"; }
      try
      {
          String connectionString = "DataSource=" + _databasename + "; Password=" + _password;
          SqlCeConnection conn = new SqlCeConnection(connectionString);
  
          if (conn.State==ConnectionState.Closed) { conn.Open(); }
  
          using (SqlCeCommand command = conn.CreateCommand())
          {
              command.CommandType = CommandType.Text;
              command.CommandText = "SELECT * FROM Information_Schema.Tables WHERE TABLE_NAME = '" + _tablename + "'";
              Int32 count = Convert.ToInt32(command.ExecuteScalar());
  
              if (count == 0)
              {
                  Debug.WriteLine("Table " + _tablename + " does not exist.");
                  return false;
              }
              else
              {
                  Debug.WriteLine("Table " + _tablename + " exists.");
                  return true;
              }
          }
      }
      catch(Exception _ex)
      {
          Debug.WriteLine("Failed to determine if table " + _tablename + " exists: " + _ex.Message);
          return false;
      }
  }
  </code></pre>
  
  <p>There clearly is something I am missing here, but I just can't seem to find out what that is.</p>