Philip Wade
Philip Wade

Reputation: 358

Making a copy of an azure database

I need to make a copy of an azure database but I don't have access to any management tools, only a connection string.

I know that to use the CREATE DATABASE x AS COPY OF y command I need to be in the master database but I don't know how to tell Azure to run my command from there. For example, I have this code:

var commandText = "CREATE DATABASE foobar AS COPY OF " + database;
var connectionString = <my_connection_string>;

using (var conn = new SqlConnection(connectionString))
{
    using (var cmd = new SqlCommand(commandText, conn))
    {
        cmd.CommandType = CommandType.Text;
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

which fails with error "User must be in the master database" - as I expected.

However my connection string is not valid for the master database, so how do I execute this command from the master database? If I was in SQL Management Studio I would just do:

use master
CREATE DATABASE x AS COPY OF y

Is there any way of emulating that in my code?

Upvotes: 2

Views: 1896

Answers (1)

Change your connection string to access the master database instead of your regular one. It's as simple as that and the only solution.

Upvotes: 4

Related Questions