Question

I'm attempting to copy one Azure SQL Database to another, located on the same server. I'm doing this using SMO (System.Data.SqlClient). I know this is not the most efficient way, however my intention is to utilize this script to create a scheduled copy of my Azure SQL Database on my on-premise SQL Server.

Here's my code (Dev is the original Database, Dev-Mirror is the destination database):

var serverConnection = new ServerConnection();
        serverConnection.NetworkProtocol = NetworkProtocol.TcpIp;
        serverConnection.ConnectionString = "Server=tcp:mysernamename.database.windows.net,1433;User ID=user@server;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";
        var server = new Server(serverConnection);
        var transfer = new Transfer(server.Databases["Dev"]);
        transfer.CopyAllObjects = true;
        transfer.CopyAllUsers = true;
        transfer.Options.WithDependencies = true;
        transfer.DestinationDatabase = server.Databases["Dev-Mirror"].Name;
        transfer.DestinationServer = server.Name;
        transfer.DestinationLoginSecure = true;
        transfer.CopySchema = true;
        transfer.CopyData = true;
        transfer.Options.ContinueScriptingOnError = true;
        transfer.TransferData();

After two minutes or so, this code throws the following TransferException after executing TransferData (however, the connection to the server itself is successful, and the Databases property on the Server instance are populated:

Microsoft.SqlServer.Management.Common.TransferException was unhandled
  HResult=-2146233088
  Message=An error occurred while transferring data. See the inner exception for details.
  Source=Microsoft.SqlServer.SmoExtended
  StackTrace:
       at Microsoft.SqlServer.Management.Smo.Transfer.TransferData()
       at SmoDatabaseCopy.Program.Main(String[] args) in c:\Users\bronf_000\Documents\Visual Studio 2013\Projects\SmoDatabaseCopy\SmoDatabaseCopy\Program.cs:line 32
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Data.SqlClient.SqlException
       HResult=-2146232060
       Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
       Source=.Net SqlClient Data Provider
       ErrorCode=-2146232060
       Class=20
       LineNumber=0
       Number=2
       Server=""
       State=0
       StackTrace:
            at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
            at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
            at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
            at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
            at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
            at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
            at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
            at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
            at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
            at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
            at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
            at System.Data.SqlClient.SqlConnection.Open()
            at Microsoft.SqlServer.Management.Smo.Transfer.TransferData()
       InnerException: System.ComponentModel.Win32Exception
            HResult=-2147467259
            Message=The system cannot find the file specified
            ErrorCode=-2147467259
            NativeErrorCode=2
            InnerException: 
Was it helpful?

Solution 2

We ended up using the Microsoft.Sql.Dac library, which allows performing full backup & restore (BAKPAK and BACPAC files) to a database of your choice. This works flawlessly with Azure.

OTHER TIPS

Okay, so apparently the answer is easier than I thought. According to this, Microsoft have only implemented a partial set of SMO on Azure SQL, to the extent of supporting SQL Server Management Studio which utilizes this API. Actual use of the SMO API on Azure SQL is not recommended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top