I am trying to copy everything (data, indexes, triggers, stored procedure). From one database to another in C#.
Here is my code:
SqlConnection connection = new SqlConnection(ConnectionString);
Server myServer = new Server(new ServerConnection(connection));
Database db = myServer.Databases[this._myDB];
if (myServer.Databases[this._newDB] != null)
myServer.Databases[this._newDB].Drop();
Database newdb = new Database(myServer, this._newDB);
newdb.Create();
Transfer transfer = new Transfer(db);
transfer.CopyAllSchemas = false;
transfer.CopyAllStoredProcedures = true;
transfer.CopyAllTables = true;
transfer.CopyAllDatabaseTriggers = true;
transfer.CopyAllObjects = true;
transfer.CopyAllUsers = true;
transfer.Options.WithDependencies = true;
transfer.DestinationDatabase = newdb.Name;
transfer.DestinationServer = myServer.Name;
transfer.DestinationLoginSecure = false;
transfer.DestinationLogin = user;
transfer.DestinationPassword = pwd;
transfer.CopySchema = false;
transfer.CopyData = true;
transfer.Options.DriAll = true;
transfer.Options.Triggers = true;
transfer.Options.WithDependencies = true;
transfer.Options.ContinueScriptingOnError = true;
transfer.TransferData();
I get the following error:
errorCode=-1071636471 description=SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "Login failed for user 'DOMAIN\user'.".
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "Cannot open database "myDB(replacing name, but it is correct)" requested by the login. The login failed.".
helpFile= helpContext=0 idofInterfaceWithError={5BC870EB-BBA5-4B9D-A6E3-58C6D0051F14}
The closest I have come to accomplishing it with a workaround is by doing:
script = transfer.ScriptTransfer();
foreach (string s in script)
{
//run a sqlcommand for s
}
But this doesn't get the data.
I have given the user every permission I can think of both for SQL Server and for the database itself.
Its too late but it could help some body else,
I had the same problem and its due to the first line, the cast to the SqlConnection
. It activates the mixed authentication [thats why you got an error as your login is wrong for 'DOMAIN\user']
I've choose to initialize a simple connection using the SqlConnectionStringBuilder
:
var connectionString = ((EntityConnection)base.ReferentielContext.Connection).StoreConnection.ConnectionString;
var builder = new SqlConnectionStringBuilder(connectionString);
var srvConn = new ServerConnection
{
ServerInstance = builder.DataSource,
LoginSecure = false,// set to true for Windows Authentication
Login = builder.UserID,
Password = builder.Password
};
var server = new Microsoft.SqlServer.Management.Smo.Server(srvConn);
来源:https://stackoverflow.com/questions/16530120/need-help-connection-with-microsoft-sqlserver-management-smo-transfer-class