2011-03-04 42 views
2

我在C#應用程序中使用SQL Server管理對象庫。我需要將存儲過程從源服務器複製到另一臺計算機上的目標服務器。我可以從源服務器中檢索StoredProcedure對象並在調試器(transferProc)中查看對象。將存儲過程從一個SQL Server 2008複製到另一個使用SQL Server管理對象

如果我嘗試只是添加PROC(transferProc)到新服務器上存儲過程對象:

tdb.StoredProcedures.Add(transferProc); 

當我這樣做,我得到一個錯誤,指出:

Message "Parent property of object [dbo].[aStoredProc] does not match the collection's parent to which it is added."

如果我嘗試並更改父,並將其設置到目標數據庫,我得到一個不同的錯誤:

Message "SetParent failed for StoredProcedure 'dbo.aStoredProc'. "

InnerException {"Cannot perform the operation on this object, because the object is a member of a collection."}

如何複製ŧ他db.StoredProcedure對象到新服務器上的tdb.StoredProcedure?

using Microsoft.SqlServer.Management.Smo; 
    using Microsoft.SqlServer.Server; 
    using Microsoft.SqlServer.Management.Common; 
    using Microsoft.SqlServer; 

    string srcDB = "foo"; 
    string destDB = "bar"; 
    string proc = "aStoredProc"; 

    __DevSqlConnection = new SqlConnection(__DevSqlConnectionString); 
    __DevSqlConnection.Open(); 

    __TestingSqlConnection = new SqlConnection(__TestingSqlConnectionString); 
    __TestingSqlConnection.Open(); 

    //SMO Server object setup with SQLConnection. 
    Server devServer = new Server(new ServerConnection(__DevSqlConnection)); 
    //Set Database 
    Database db = devServer.Databases[srcDB]; 

    //SMO For the Receiving Server 
    Server testServer = new Server(new ServerConnection(__TestingSqlConnection)); 
    //Set Database 
    Database tdb = testServer.Databases[destDB]; 

    //Set the proc we wish to Script 
    StoredProcedure transferProc = db.StoredProcedures[proc]; 

    //Change the parent 
    transferProc.Parent = tdb; 

    tdb.StoredProcedures.Add(transferProc); 
+0

沒有「C#.NET」這樣的東西。只有「C#」。你在哪看到「C#.NET」? – 2011-03-04 02:41:54

+0

啊,正確的,只是意識到這段代碼中沒有.NET部分。 – withaK 2011-03-04 03:34:03

+0

MS SQL 2008 R2不再支持FYI SQL DMO。 – 2011-03-04 05:02:25

回答

3

我發現如果您創建一個新的存儲過程實例與目標數據庫作爲父,並設置過程的名稱和架構,然後設置TextBody和TextHeader,然後調用create方法它將從一個複製存儲過程數據庫到另一個。

StoredProcedure procNew = new StoredProcedure(dbProd, procDEV.Name, procDEV.Schema); 
procNew.TextBody = procDEV.TextBody; 
procNew.TextHeader = procDEV.TextHeader; 
procNew.Create(); 
0

試試這個。請注意,

procedureText:是實際的存儲過程代碼,而不是存儲過程的名稱。 的databaseName:這是

//Create a new StoredProcedure Object 
StoredProcedure sProcedure = new StoredProcedure(); 
//Set its Text property to the 
//text of the procedure you're creating 
sProcedure.Text = procedureText; 
//Add the stored procedure to the database 
Server.Databases.Item(databaseName, "dbo").StoredProcedures.Add(sProcedure); 
+0

沒有工作。 StoredProcedure()上沒有「文本」屬性。有'TextBody','TextHeader'和'TextMode'。我最終開始工作,但我並不滿意。我使用Script()方法生成腳本,然後必須解析出前兩行,因爲運行時腳本必須以'CREATE或ALTER'開頭。 – withaK 2011-03-04 22:26:30

+0

您是否嘗試過TextBody? – 2011-03-05 00:01:49

+0

這很奇怪,因爲我有使用Text屬性創建存儲過程的代碼。你使用的是什麼版本的dmo庫? – 2011-03-05 00:09:02

0

我最終得到以工作數據庫的名字,但我並不感到它。我使用Script()方法生成一個腳本,然後必須解析出前兩行,因爲腳本運行時必須以'CREATE或ALTER'開頭。

using Microsoft.SqlServer.Management.Smo; 
    using Microsoft.SqlServer.Server; 
    using Microsoft.SqlServer.Management.Common; 
    using Microsoft.SqlServer; 

    string srcDB = "foo"; 
    string destDB = "bar"; 
    string proc = "aStoredProc"; 


__DevSqlConnection = new SqlConnection(__DevSqlConnectionString); 
       __DevSqlConnection.Open(); 

       __TestingSqlConnection = new SqlConnection(__TestingSqlConnectionString); 
       __TestingSqlConnection.Open(); 

       //SMO Server object setup with SQLConnection. 
       Server devServer = new Server(new ServerConnection(__DevSqlConnection)); 
       //Set Database 
       Database db = devServer.Databases[srcDB]; 

       //SMO For the Receiving Server 
       Server testServer = new Server(new ServerConnection(__TestingSqlConnection)); 
       //Set Database 
       Database tdb = testServer.Databases[destDB]; 

       //Set the proc we wish to Script 
       StoredProcedure transferProc = db.StoredProcedures[proc]; 
       transferProc.TextMode = false; 
       transferProc.AnsiNullsStatus = false; 
       transferProc.QuotedIdentifierStatus = false; 

       //Create an SPObj for the new server. 
       StoredProcedure tProc = new StoredProcedure(tdb, proc); 


       //Create the Creation Script. 
       ScriptingOptions so = new ScriptingOptions(); 
       StringCollection script = transferProc.Script(so); 
       string[] scriptArray = new string[script.Count]; 
       script.CopyTo(scriptArray, 0); 
       foreach (string line in scriptArray) 
       { 
        if (line.IndexOf("CREATE", StringComparison.CurrentCulture) >= 0) 
         __SchemaScript += line + Environment.NewLine; 
       } 


       //Run the script against the target testing DB. 
       SqlCommand cmdCreate = new SqlCommand(__SchemaScript, __TestingSqlConnection); 
       cmdCreate.CommandType = CommandType.Text; 
       cmdCreate.CommandTimeout = 7200; 
       cmdCreate.ExecuteNonQuery(); 
0

你接近: '獲取從 「從」 服務器的數據庫 昏暗FRSP的String = getSP(DBF,theSP,FailF,paramsF) 的SP文本' 在SP的 「收件人」 服務器和文本/腳本 spT.TextBody = FRSP spT.TextMode =假

對於PARAMS: 昏暗paramsF作爲StoredProcedureParameterCollection = spT.Parameters

如果不paramsF是Nothing然後 對於各個p作爲StoredProcedureParameter在paramsF 昏暗PP爲StoredProcedureParameter 第=新StoredProcedureParameter(SPT,p.Name,p.DataType) spT.Parameters.Add(PP) 第=無 接着

End If 

HTH

相關問題