如何以编程方式获取 SSIS 包中的 MS Access 表列表?

本文介绍了如何以编程方式获取 SSIS 包中的 MS Access 表列表?的处理方法,对大家解决问题具有一定的参考价值

问题描述

我继承了一个非常编写的 MS Access 数据库,我需要将其导入 SQL.Access 数据库中有数千个具有相同字段定义的表.我对 SSIS 有一些经验,导入一张表非常简单.

I have inherited a terribly written MS Access database that I need to import into SQL. The Access database has several thousand tables in it with field definitions that are identical. I have some experience with SSIS, and importing one table is pretty simple.

但是,我需要创建一个进程,我可以在其中循环遍历数千个表名的列表并导入每个表.我找到了这个语句,它将获得 Access 数据库中所有表名的列表:

However, I need to create a process where I can loop through the list of several thousand table names and import each table. I found this statement, that will get a list of all the table names in an Access database:

SELECT Name FROM MSysObjects WHERE (((MSysObjects.Type)=1) AND ((Left([Name],4))<>"MSys")) ;

SELECT Name FROM MSysObjects WHERE (((MSysObjects.Type)=1) AND ((Left([Name],4))<>"MSys")) ;

但是,我不确定如何使用它(脚本任务语法?).我想我会想要这样做来填充对象"类型的 SSIS 变量.这样,我可以使用 ForEach 循环循环浏览此表列表并执行导入.我怎样才能做到这一点?或者有没有更好的方法来循环遍历数据库中的每个表并执行相同的过程?

However, I am unsure of how to use this (script task syntax?). I would think I would want to do this to populate a SSIS variable of an "object" type. That way, I can use a ForEach Loop to cycle through this list of tables and perform the importing. How can I do this? Or is there a better way to cycle through each table in the database and perform the same process?

我将不胜感激任何建议.谢谢!

I would greatly appreciate any suggestions. Thanks you!

推荐答案

只要 Access 中的所有表具有相同的结构,就可以实现将 Access 数据加载到 SQL Server 中的一种可能方法.此示例将遍历 Access 中的表,即 CountryStateProvince.如果这两个表不存在,本示例中的包将在 SQL 中创建这两个表,然后用 Access 中的数据填充它们.

Here is one possible way that you can achieve loading Access data into SQL Server as long as all the tables in Access have the same structure. This example will loop through tables in Access namely Country and StateProvince. The package in this example will create these two tables in SQL if they don't exist and then populate them with data from Access.

分步过程:

  1. 访问表CountryStateProvince 显示在屏幕截图#1 和#2 中.

  1. Access tables Country and StateProvince are shown in screenshots #1 and #2.

在 SSIS 包上,创建两个 OLE DB 连接以连接到 SQL Server 和 Access,如屏幕截图 #3 所示.此外,创建 3 个变量,如屏幕截图 #4 所示.变量 SelectQueryTableName 应由 Access 中的有效表指定.这是包的初始配置所必需的.在本例中,我选择了 Country,它确实存在于 Access 中.

On the SSIS package, create two OLE DB Connections to connect to SQL Server and Access as shown in screenshot #3. Also, create 3 variables as shown in screenshot #4. Variables SelectQuery and TableName should be specified by a valid table in Access. This is needed for initial configuration of the package. Here in this case, I have chosen Country, which does exist in Access.

选择变量 SelectQuery 并按 F4 查看属性窗格.在属性"窗格上,将属性 EvaluateAsExpress 设置为 True 并粘贴表达式 "SELECT * FROM " + @[User::TableName]<!--Expression 属性中的 code>.此表达式将计算当前正在循环的表.请参阅屏幕截图 #4

Select the variable SelectQuery and press F4 to view the properties pane. On the Properties pane, set the property EvaluateAsExpress to True and paste the expression "SELECT * FROM " + @[User::TableName] in the Expression property. This expression will evaluate to the table that is currently being looped through. Refer screenshot #4

屏幕截图 #5 和 #6 显示表 dbo.Countrydbo.StateProvince> 在 SQL Server 中不存在.

Screenshots #5 and #6 show that the tables dbo.Country and dbo.StateProvince do not exist in SQL Server.

配置 SSIS 包的 Control Flow 选项卡,如屏幕截图 #7 所示.放置一个 Script Task 并将其连接到 Foreach Loop 容器.在容器内,放置一个 Execute SQL Task 和一个 Data Flow Task.

Configure the Control Flow tab of the SSIS package as shown in screenshot #7. Place a Script Task and connect it to a Foreach Loop container. Within the container, place an Execute SQL Task and a Data Flow Task.

用脚本任务代码部分下给出的代码替换脚本任务中的代码.此代码将循环访问模式并仅获取表名.然后将表名列表存储在包变量 AccessTables 中,然后由 Foreach 循环容器 使用.

Replace the code in the Script Task with the code given under the Script Task Code section. This code will loop the Access schema and will fetch only the table names. The list of table names are then stored in the package variable AccessTables, which will then used by Foreach loop container.

在 SQL Server 数据库中,使用 SQL 脚本 部分下提供的脚本创建一个名为 dbo.CreateTable 的存储过程.如果 SQL Server 尚不存在,则此存储过程将在 SQL Server 中创建该表.确保根据需要更改存储过程中定义的表架构.

In the SQL Server database create a stored procedure named dbo.CreateTable using the script provided under SQL Scripts Section. This stored procedure will create a table in the SQL Server if it didn't already exist. Make sure that you alter the table schema defined in the stored procedure according to your needs.

配置 Foreach 循环容器,如屏幕截图 #8 和 #9 所示.

Configure the Foreach loop container as shown in screenshots #8 and #9.

配置执行 SQL 任务,如屏幕截图 #10 和 #11 所示.

Configure the Execute SQL Task as shown in screenshots #10 and #11.

此时我们无法配置数据流任务,因为 SQL Server 中不存在这些表.因此,我们将在此时执行包,以便在 SQL Server 中创建 Access 表结构.屏幕截图 #12 显示了示例包执行.屏幕截图 #13 显示表结构已在 SQL Server 中创建,但尚未填充数据.

We cannot configure Data Flow Task at this point because the tables don't exist in SQL Server. So, we will execute the package at this point so the Access table structures are created in the SQL Server. Screenshot #12 shows sample package execution. Screenshot #13 shows that the table structures have been created in SQL Server but they are not yet populated with data.

现在,我们将配置数据流任务.在数据流任务中放置一个 OLE DB SourceOLE DB Destination.将 OLE DB 源连接到 OLE DB 目标.请参阅屏幕截图 #14.

Now, we will configure the Data Flow Task. Place an OLE DB Source and OLE DB Destination inside the Data Flow Task. Connect the OLE DB Source to OLE DB Destination. Refer screenshot #14.

配置 OLE DB Source,如屏幕截图 #15 和 #16 所示.

Configure the OLE DB Source as shown in screenshots #15 and #16.

配置 OLE DB 目标,如屏幕截图 #17 和 #18 所示.

Configure the OLE DB Destination as shown in screenshots #17 and #18.

屏幕截图 #19 显示了 Data Flow Task 中的示例包执行.

Screenshot #19 shows sample package execution within Data Flow Task.

屏幕截图 #20 显示 SQL Server 表现在填充了来自 Access 表的数据.

Screenshot #20 shows that SQL Server tables are now populated with data from Access tables.

此示例仅适用于具有相同结构但名称不同的表.如果另一个名为 Employees 的表被添加到 Access 中,只有列 IdName.执行此示例程序包将在 SQL Server 中创建相同的表,并将用数据填充它.

This example will work only for tables having the same structure but differing in the name. If another table named Employees are added to the Access with only columns Id and Name. Executing this example package will create the same table in SQL Server and will also populate it with the data.

希望有所帮助.

SQL 脚本:

CREATE PROCEDURE [dbo].[CreateTable]
(
    @TableName  VARCHAR(255)
)
AS
BEGIN

    SET NOCOUNT ON

    DECLARE @SQL VARCHAR(MAX)

    SET @SQL = 'IF NOT EXISTS ( SELECT  * 
                                FROM    sys.objects 
                                WHERE   object_id = OBJECT_ID(N''[dbo].' + @TableName + ''') 
                                AND     type in (N''U''))
                    CREATE TABLE [dbo].' + @TableName + '(
                        [ID] [int] NOT NULL,
                        [Name] [nvarchar](255) NULL
                        ) ON [PRIMARY]'

    EXEC (@SQL)
END
GO

脚本任务代码:

C# 代码只能在 SSIS 2008 及更高版本中使用.

C# code that can be used only in SSIS 2008 and above.

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_9b2714c55db14556be74ca92f345c4e3.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        public void Main()
        {
            Variables varCollection = null;
            DataTable schemaTables = null;
            ArrayList tableNames = new ArrayList();

            Dts.VariableDispenser.LockForWrite("User::AccessTables");
            Dts.VariableDispenser.GetVariables(ref varCollection);

            using (OleDbConnection connection = new OleDbConnection(Dts.Connections["AccessDB"].ConnectionString.ToString()))
            {
                string[] restrictions = new string[4];
                restrictions[3] = "Table";    
                connection.Open();
                schemaTables = connection.GetSchema("Tables", restrictions);
            }

            foreach (DataRow row in schemaTables.Rows)
            {
                foreach (DataColumn column in schemaTables.Columns)
                {
                    if (column.ColumnName.ToUpper() == "TABLE_NAME")
                    {
                        tableNames.Add(row[column].ToString());
                    }
                }
            }

            varCollection["User::AccessTables"].Value = tableNames;

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

屏幕截图 #1:

屏幕截图 #2:

屏幕截图 #3:

屏幕截图 #4:

屏幕截图 #5:

截图 #6:

屏幕截图 #7:

截图 #8:

屏幕截图 #9:

屏幕截图 #10:

屏幕截图 #11:

屏幕截图 #12:

屏幕截图 #13:

屏幕截图 #14:

屏幕截图 #15:

屏幕截图 #16:

屏幕截图 #17:

屏幕截图 #18:

屏幕截图 #19:

屏幕截图 #20:

这篇关于如何以编程方式获取 SSIS 包中的 MS Access 表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1159

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1060

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:800

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:887

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:873

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:855

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1691

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1298