Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hbulens/7065354440a4b83406d2954ab24ffa8d to your computer and use it in GitHub Desktop.
Save hbulens/7065354440a4b83406d2954ab24ffa8d to your computer and use it in GitHub Desktop.
private static bool IsTenantProvisioned(string tenantSchema, DbConnection connection)
{
// Execute plain SQL query to check for tenant schema existence
using(SqlConnection sqlConnection = new SqlConnection(connection.ConnectionString))
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = sqlConnection;
cmd.CommandText = string.Format("SELECT COUNT(*) FROM sys.schemas WHERE name = '{0}'", tenantSchema);
cmd.CommandType = CommandType.Text;
try
{
sqlConnection.Open();
int total = (int) cmd.ExecuteScalar();
if (total > 0)
{
return true;
} else
{
return false;
}
} catch (SqlException e)
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment