Sunday, February 19, 2012

How I can copy object SMO between DB and servers?

Something following is necessary:

Column c1 = srv1.Databases[“db1”].Tables[“t1”].Columns[“c1”];

Column c2 = <something which copies c1>

srv2.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;

srv2.ConnectionContext.CapturedSql.Clear();

srv2.Databases[“db1”].Tables[“t1”].Columns.Add( c2 );

foreach( string s in srv2.ConnectionContext.CapturedSql.Text )

{

Debug.WriteLine( s );

}

srv2.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql;

Really it is necessary to make copying all properties of c1 in {c2 = new Column()} ?

And the similar question: How to make so that the existing column in db1 would begin by the same as in db2? And to obtain the script of this.

I write the program of the comparison of DB structures. Comparison already works. But here with scripting arose problems.

I think you try to generate the change script. Setting all properties is needed in that case.

|||

Thanks, Michiel

I searched for the universal method of copying, for example the properties of column. Something like this:

Column c1 = db1.Tables[“t1”];

Column c2 = new Column();

c2.Parent = db2.Tables[“t2”];

c2.CopyPropertiesFrom( c1 );

or

c2.ToMakeSimilarOn( c1 );

Such is necessary in my case. It is to be regretted that it does not exist.

I tried to write something like:

Column c1 = srv.Databases["db1"].Tables["t1"].Columns["col"];

Column c2 = new Column( srv.Databases["db2"].Tables["t1"], c1.Name );

c2.DataType = c1.DataType;

foreach( Property p in c1.Properties )

{
if( !p.Dirty && p.Readable && p.Retrieved && p.Writable && !p.IsNull )

{

c2.Properties[p.Name].Value = p.Value;

}

}

But, as it proved to be - this is incorrect approach.

And I, enormous thanks for your Weblog.

No comments:

Post a Comment