2016-08-22 1 views
1

SMO를 사용하여 한 데이터베이스에서 다른 데이터베이스로 데이터를 복사하고 있습니다. 내 자신의 DB 헬퍼 클래스를 사용했습니다. 데이터를 전송할 때 예외가 발생합니다.Integration Services 구성 요소가 설치되어 있지 않거나 사용 권한이 없습니다.

Integration Services 구성 요소가 설치되어 있지 않거나 사용 권한이 없습니다.

public class DBHelper 
{ 
    #region Private Variables 
    private static string sourceSQLServer; 
    private static string destinationSQLServer; 
    private static string sourceDatabase; 
    private static string destinationDatabase; 
    #endregion 

    #region Properties 

    /// <summary> 
    /// SourceSQLServer Holds Instance Name of Source SQL Server Database Name 
    /// </summary> 
    public static string SourceSQLServer 
    { 
     get { return DBHelper.sourceSQLServer; } 
     set { DBHelper.sourceSQLServer = value; } 
    } 

    /// <summary> 
    /// DestinationSQLServer Holds Instance Name of Destination SQL Server Database Name 
    /// </summary> 
    public static string DestinationSQLServer 
    { 
     get { return DBHelper.destinationSQLServer; } 
     set { DBHelper.destinationSQLServer = value; } 
    } 

    /// <summary> 
    /// SourceDatabase Holds Source Database 
    /// </summary> 
    public static string SourceDatabase 
    { 
     get { return DBHelper.sourceDatabase; } 
     set { DBHelper.sourceDatabase = value; } 
    } 

    /// <summary> 
    /// DestinationDatabase Holds Destination Database Name 
    /// </summary> 
    public static string DestinationDatabase 
    { 
     get { return DBHelper.destinationDatabase; } 
     set { DBHelper.destinationDatabase = value; } 
    } 
    #endregion 

    #region Static Methods 
    /// <summary> 
    /// CopyDatabase Copies Database 
    /// </summary> 
    /// <param name="CopyData">True if Want to Copy Data otherwise False</param> 
    public static void CopyDatabase(bool bCopyData) 
    { 
     //Set Source SQL Server Instance Information 
     Server server = new Server(DBHelper.SourceSQLServer); 

     //Set Source Database Name [Database to Copy] 
     Database database = server.Databases[DBHelper.SourceDatabase]; 

     //Set Transfer Class Source Database 
     Transfer transfer = new Transfer(database); 

     //Yes I want to Copy All the Database Objects 
     transfer.CopyAllObjects = true; 

     //In case if the Destination Database/Objects Exists Drop them First 
     transfer.DropDestinationObjectsFirst = true; 

     //Copy Database Schema 
     transfer.CopySchema = true; 

     //Copy Database Data Get Value from bCopyData Parameter 
     transfer.CopyData = bCopyData; 

     //Set Destination SQL Server Instance Name 
     transfer.DestinationServer = DBHelper.DestinationSQLServer; 

     //Create The Database in Destination Server 
     transfer.CreateTargetDatabase = true; 

     //Set Destination Database Name 
     Database ddatabase = new Database(server, DBHelper.DestinationDatabase); 

     //Create Empty Database at Destination 
     ddatabase.Create(); 

     //Set Destination Database Name 
     transfer.DestinationDatabase = DBHelper.DestinationDatabase; 

     //Include If Not Exists Clause in the Script 
     transfer.Options.IncludeIfNotExists = true; 

     //Start Transfer 
     transfer.TransferData(); 

     //Release Server variable 
     server = null; 
    } 
    #endregion 
} 

및 주요 기능에 내가 미리이

DBHelper.SourceSQLServer = @"DESKTOP-PCEOPRM\SQLEXPRESS"; 

//Set Your Database Name Here (To Be Copied or Scripted) 
DBHelper.SourceDatabase = "MetisEmptyDB"; 

////Set Destination SQL Server Name Here 
DBHelper.DestinationSQLServer = @"DESKTOP-PCEOPRM\SQLEXPRESS"; 

//Set New Database Name Here 
DBHelper.DestinationDatabase = "NewDb"; 

//Set True if you want to copy Data 
//Set False if you want to copy Only Schema 
DBHelper.CopyDatabase(true); 
Console.WriteLine("Scripting Finished"); 

감사를 호출하고 다음과 같이

내 코드입니다. C#의 기존 데이터베이스에서 새 db를 만드는 다른 대체 방법을 찾으면 코드도 공유하십시오.

답변

0

실마리가 잘못되었습니다. SQL Server Integration Services 용 코드를 사용하려고하지만 SQL Server Express에는 포함되어 있지 않습니다.

또한 이러한 것들이 T-SQL을 사용하면 훨씬 쉽습니다. 쉽게 빈 데이터베이스를 새로 만들고 개체를 스크립팅 할 수도 있습니다. 또는 데이터베이스를 백업하고 복원하십시오.

내가 더 잘 dba.stackexchange.com

BACKUP DATABASE AdventureWorks2012 
TO DISK = 'C:\SQLServerBackups\AdvWorksData.bak' 
WITH FORMAT; 

에 메시지가 표시됩니다으로 유래에 여기에 코드를 게시하지 말아야하고, 다른 서버에 복원

RESTORE DATABASE AdventureWorks2012 
    FROM DISK = 'C:\SQLServerBackups\AdventureWorks2012.bak'; 

있는지 확인하십시오 당신 C#을 통해 SQL을 사용하기 전에 수행중인 작업을 이해하십시오. 이전 질문에서 권장 한대로 SQLCMD를 사용하여 모든 SQL을 수동으로 수행 한 경우 SQL에 대해 더 많이 배우게됩니다.

+0

저는이 질문을 여기서도 사용했지만 사람들이 투표를하는 것을 돕는 대신에이 코드를 사용했습니다. 나는 사람들이 왜 초심자와 그렇게하는지 모른다. 도움을받을 수 있다면이 질문을 참조하십시오. @Mike http://stackoverflow.com/questions/39019461/run-a-script-on-existing-database-from-sql-file-in-c-sharp/39019576 – Umar

+0

내가 가장 간단한 SQL 예제를 추가했습니다. 데이터베이스 백업 및 복원. C# – Mike

+0

감사합니다. @ 마이크, 가장 간단했습니다. 세 가지 방법 중 하나를 읽었습니다. 하나는 SMO이고, 두 번째는 제안한 것이고 세 번째는 SQLcmd를 사용하는 프로세스 메서드입니다. 희망이 하나는 작동합니다! – Umar

0

SQL Server 2012 Express에서 SQL Server 2012 Express로 종속성을 변경하면이 오류가 사라졌습니다.

나는하지

C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies 

당신이 작동 한 후 종속성을 변경 할 수있는 경우

C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies 

에서 어셈블리를 참조.

관련 문제