简单描述或指南:smo将对象的模式从源传输到目标 [英] Simple description or guide: smo transferring schema of objects from source to destination

查看:66
本文介绍了简单描述或指南:smo将对象的模式从源传输到目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我一直在开发一个简单的向导应用程序,它将对象的架构从源映射到目标数据库。而且,我使用SMO(Microsoft.SqlServer.Management.Smo)库来进行映射。它为表,视图,存储过程甚至字段/列创建,如果它们在目标数据库中不存在或丢失。



我遇到的问题是,如何在目标表中移动或创建列字段,其中所有属性都是从源数据库中找到的?



我想要包含列约束,在表中的位置,以及来自源表的其他默认属性。



先谢谢



< b>我尝试了什么:



表tb = dbSource.Tables [field.Parent];

Microsoft.SqlServer .Management.Smo.Column column = tb.Columns [field.Name];





表tblDest = dbDestination.Tables [tb。姓名];



if(column!= null& tblDest!= null)

{

尝试

{

Microsoft.SqlServer.Management.Smo.Column newColumn =

new Microsoft.SqlServer .Management.Smo.Column(tblDest,column.Name,

column.DataType)

{

DefaultSchema = column.DefaultSchema,



};



tblDest.Columns.Add(newColumn);

}

catch(例外e)

{



}



尝试

{

//持续更改回数据库

tblDest.Alter();

}

catch(SmoException ex)

{

抛出ex;

}

}

Dear all,

I have been developing a simple wizard application which maps object's schema from source to a destination database. And, I used SMO (Microsoft.SqlServer.Management.Smo) library to do the mappings. It creates for tables, views, storedprocedures, and even fields/columns, if they are not existed or missed at destination database.

The problem I encountered is that, how to move or create a column field at destination table with all its properties found from source database?

I wanted to include the columns constraints, position in table, and other default properties that come from source table.

Thanks in Advance

What I have tried:

Table tb = dbSource.Tables[field.Parent];
Microsoft.SqlServer.Management.Smo.Column column = tb.Columns[field.Name];


Table tblDest = dbDestination.Tables[tb.Name];

if (column != null & tblDest != null)
{
try
{
Microsoft.SqlServer.Management.Smo.Column newColumn =
new Microsoft.SqlServer.Management.Smo.Column(tblDest, column.Name,
column.DataType)
{
DefaultSchema = column.DefaultSchema,

};

tblDest.Columns.Add(newColumn);
}
catch (Exception e)
{

}

try
{
//persist changes back to database
tblDest.Alter();
}
catch (SmoException ex)
{
throw ex;
}
}

推荐答案

查看我的改进版 SMO Tutorial 3 of n n-Scripting的SMO教程3 [ ^ ]

这包括生成密钥,索引和约束的例程。
See my improved version of "SMO Tutorial 3 of n": SMO Tutorial 3 of n - Scripting[^]
This includes routines to generate Keys, indexes and constraints.


这篇关于简单描述或指南:smo将对象的模式从源传输到目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆