我在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);
沒有「C#.NET」這樣的東西。只有「C#」。你在哪看到「C#.NET」? – 2011-03-04 02:41:54
啊,正確的,只是意識到這段代碼中沒有.NET部分。 – withaK 2011-03-04 03:34:03
MS SQL 2008 R2不再支持FYI SQL DMO。 – 2011-03-04 05:02:25