数据库构建工具






3.80/5 (5投票s)
用于打包和部署数据库架构对象的扩展工具。
引言
数据库程序集构建工具允许开发人员选择数据库模式项(表、视图、存储过程、用户等),并将创建这些项的 DDL 导出到 SQL 文件。然后,可以将此文件运行在另一个数据库上来重新创建源模式。该工具旨在可扩展,以便能够适应任何数据库平台,并且本文代码中包含了一个 SQL Server 实现来展示这一点。
DataSchemaBase
DataSchemaBase
是数据模式提供程序和数据库构建向导之间的“契约”。它定义了一组接口,数据模式提供程序必须实现这些接口才能用于生成 DDL。
IDataSource
IDataSource
类是数据模式提供程序的根。它提供有关数据模式提供程序能力的信息(是否支持存储过程、视图等),并提供对这些数据库对象的只读集合。
Public MustInherit Class IDataSourceBase
Public MustOverride ReadOnly Property Indexes() As IIndexCollection
'...8< ....
Public MustOverride ReadOnly Property SupportsViews() As Boolean
End Class
ITablebase
这定义了数据库向导需要了解的表的哪些信息——本质上是构成表的字段以及适用于该表的权限。
#Region "Fields"
'\\ --[Fields]---------------------------------------------------------
'\\ The individual fields (aka columns) in this table
'\\ -------------------------------------------------------------------
Public MustOverride ReadOnly Property Fields() As IFieldCollection
#End Region
#Region "Permissions"
'\\ --[Permissions]----------------------------------------------------
'\\ The users or user groups that have permissions on this table
'\\ -------------------------------------------------------------------
Public MustOverride ReadOnly Property Permissions() As IPermissionCollection
#End Region
此外,它提供了一个 ReadOnly
属性 DDL
,其中包含重新创建此表所需的 SQL 语句。
IViewBase...等。
其他数据库项类型的实现类都与 ITableBase
大致相似。
SQLServerDataSchema
此 DLL 包含 DataSchemaBase
类的 SQL Server 特定实现,允许您提取 SQL Server(或 MSDE)数据库的 DDL。
SQLServerDatasource
这为 SQL Server 数据库实现了 IDataSource
接口。由于此数据库平台支持列出的所有数据库项类型,因此它会为其 SupportsStoredProcedures
和相关属性返回 true,并且必须提供 Storedprocedures
和相关对象集合的实现。
SQLServerTableCollection
这个类型安全的集合返回选定数据库中所有表的列表。它通过查询 sysobjects
表来实现此目的。
Public Class SQLServerTables
Inherits ITableCollection
'..8< .....
Public Sub New(ByVal dbConn As OleDb.OleDbConnection)
'\\ Creates a collection of all the tables in the named database
Const SQL_GETTABLES = "select name from sysobjects where type = 'U'"
'..8<...
End Sub
End Class
SQLServerTable
此类返回创建特定表所需的 DDL。它通过硬编码部分(create table
)来完成此操作,然后遍历字段和权限,追加它们的 DDL。
还有类似的类实现了其他数据库对象类型(视图、存储过程等)的每个对象。
DatabaseAssemblyWizard
这是一个单窗体的 Windows 应用程序,它使用了 Divil 的 WizardControl
,允许开发人员选择并连接到数据库,然后选择要提取的对象,最后选择目标文件名,然后生成 DDL。
插件数据模式提供程序
不同类型的数据模式提供程序从应用程序配置文件读取,以便您无需重新编译应用程序即可添加更多提供程序。
<dataSchemaProvidors>
<!-- The plug in providors that implement the DataSchemabase classes
to write out the DDL for creating the database objects
on the target database platform
Key= The unique name that the providor is known by
ImplementingAssembly = The assembly that holds the providor
ImplementingClassType = The class that implements the providor
-->
<dataSchemaProvidor Key="SQL Server"
ImplementingAssembly="SQLServerDataSchema.dll"
ImplementingClassType="SQLServerDataSchema.SQLServerDatasource"/>
</dataSchemaProvidors>
步骤 1:连接到数据库
向导的第一页提供一个文本框供您输入 ODBC 连接字符串,以及一个下拉框供您选择数据模式提供程序。一旦这两个都填写完毕,它就会从所选数据库获取所有 DB 项。对于大型数据库模式,这可能需要一到两分钟。
步骤 2:选择要提取的对象
第二步是选择要提取的表、视图、用户、存储过程、触发器或用户组。在可以继续执行步骤 3 之前,您至少需要选择一个对象类型。
步骤 3:选择输出
第三步是选择要写入 DDL 的 SQL 文件名。如果文件已存在,系统会提示您是否删除它。然后,您可以使用输出文件和 OSQL 命令行在目标数据库上生成数据模式。
历史
- 2004 年 9 月 17 日:初始版本