简体   繁体   中英

How do I create an Azure SQL Database copy during runtime?

I am new to VB.NET and Azure SQL services and I have spent the last 3 days searching for an answer and cannot come up with an answer I can truly understand. Here is my scenario and problem:

I have a VB.NET application where a user will log into their account and have their own set of tables under their own Azure SQL Database. I would like my program to automatically create their database for them (using their own selected name), which would be a database copy of an existing empty Database in Azure.

I can already access the data tables in my master database, but I do not know how to make a copy of it from my program during runtime. When I run my application and it debugs the following snippit of code, I get an exception error saying "You must be logged into the master database" I'm not sure how to resolve that error from popping up as I am using the master admin account in my normal SQL connection string.

Private Sub BtnActivate_Click(sender As Object, e As RoutedEventArgs)
    If passwrd1.Password <> passwrd2.Password Then
        MsgBox("Password doesn't match Confirm Password.", vbOKOnly)
    Else
        Dim commandString = "CREATE DATABASE " & dbname.Text & " AS COPY OF DBDEV;"
        Using connection As New SqlConnection(My.Settings.DBDEVConnectionString)
            Dim command As New SqlCommand(commandString, connection)
            Try
                connection.Open()
                command.ExecuteNonQuery()
                MsgBox("DATABASE SETUP.  USE " & dbname.Text & "AS DBNAME TO CONNECT TO")
            Catch ex As Exception
                Throw ex
            End Try
        End Using
    End If
End Sub

Please help, I've been moving nowhere and everything I'm searching for doesn't give me a clear answer to this simple scenario.

After many attempts, I solved the problem: in my connection string, I had the initial catalog default to my database and so I changed it to say "master" (even though I don't have a database named master) and it performed the Database copy for me. Woohoo!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM