【问题标题】:Query of MS Access to Update into SQL Server查询 MS Access 以更新到 SQL Server
【发布时间】:2016-07-31 01:45:03
【问题描述】:

我需要从中获取数据的旧应用程序使用非常旧的 Access 数据库 (97)。供应商提供的示例代码是 VB6。 :插入恼怒的样子:

我的新应用程序已经有一个 SQL Server 数据库,其架构在很大程度上对应于 Access 数据库。所以我想在我的应用程序中做的第一件事是读取访问数据库并将其转储到 SQL Server。本质上,我想复制 4 或 5 个表。

您会认为此类事情会有一个易于遵循的示例,但我的 google-fu 无法让我找到一个。我需要以编程方式执行此操作,而不是通过升级实用程序。

【问题讨论】:

标签: .net sql-server ms-access


【解决方案1】:

当您标记您的问题 .net 时,我想使用 c# 代码

下面的类是 ImportHelper,从源导入到目标。

为源和目标定义 ConnectionString

class ImportHelper
{

    //modify connectionstring as needed
    public string SourceConnectionString { get; set; }
    public string DestinationConnectionString { get; set; }

    public ImportHelper(string sourceConnectionString, string destinationConnectionString)
    {
        SourceConnectionString = sourceConnectionString;
        DestinationConnectionString = destinationConnectionString;
    }
    public void Import(string sourceTable, string targetTable = null)
    {
        using (var sourceConnection = new OleDbConnection(SourceConnectionString))
        {
            if (string.IsNullOrEmpty(targetTable)) targetTable = sourceTable;
            sourceConnection.Open();

            // Perform an initial count on the destination table.
            var commandRowCount = new OleDbCommand("SELECT COUNT(*) FROM " + sourceTable, sourceConnection);
            long countStart = Convert.ToInt32(commandRowCount.ExecuteScalar());
            Console.WriteLine("Source Table [{0}] has {1} rows", sourceTable, countStart);

            // Get data from the source table  
            var commandSourceData = new OleDbCommand("SELECT * FROM " + sourceTable, sourceConnection);
            var reader = commandSourceData.ExecuteReader();

            //---------------
            using (SqlConnection destinationConnection = new SqlConnection(DestinationConnectionString))
            {
                destinationConnection.Open();

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
                {
                    bulkCopy.DestinationTableName = targetTable;

                    try
                    {
                        // Write from the source to the destination.
                        bulkCopy.WriteToServer(reader);
                        Console.WriteLine("Sucess Importing " + sourceTable);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        reader.Close();
                    }
                }//using
            }//using
        }//using
    }
}

使用方法:

//modify connectionstring as needed
    //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=; //access 97..2000
    string SourceConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\database1.accdb;Persist Security Info=False;";
    string DestinationConnectionString = @"Data Source=xxxx;Initial Catalog=test;user=xxx;password=xxx;";
   new ImportHelper(SourceConnectionString, DestinationConnectionString)
     .Import("table1","test1");

【讨论】:

    【解决方案2】:

    使用 SQL Server 内置的数据导入功能,将 Access 中的数据直接导入 SQL Server。

    看这里:

    https://msdn.microsoft.com/en-us/library/ms140052.aspx

    涉及多个步骤,但我假设您只执行一次,这是将 Access 数据导入 SQL Server 的最简单方法。

    如果您需要重复执行此操作(即每周一次或其他重复的计划),设置 SSIS 包是可行的方法。

    【讨论】:

    • 唉,我不会只这样做一次。我在应用程序初始化时这样做,所以如果我可以将它作为一个进程启动,我会接受,但在我运行之前我什至不知道数据库的名称。
    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多