MS access 数据定时导入MS SQL Server

有一个程序,是记录公司域用户打印记录,它使用的是MS Access database(A服务器 32bit)。想以这些记录做一些Summary报表,报表实现是另外一台服务器Intranet web 服务(B服务器 64bit)。如查在非一台服务器读取远端的Access数据,也许需要数据库文件共享。网管说不能在服务器安装32 bit的Ole 驱动,不也能共享A服务器的Access数据库文件。

Insus.NET需要怎样做?
只有想法是把这些记录每天定时转存一次至公司的数据库(MS SQL Server 2008 R2)(C服务器)。写一个控制台应用程序,放在Access数据库同一目录,然后让A服务器的任务定时执行这控制台应用程序。

在数据库设计一张表,表结构与Access需要转存记录的表结构一样。 只有第一次转存时,数据量大的话,时间需要长久一些,以后每次只转存SQL 数据库内没有的数据。

MS access 数据定时导入MS SQL Server View Code
CREATE TABLE [dbo].[PrintLog]

(

    [ID] INT PRIMARY KEY NOT NULL,

    --other field list

)



GO


为了解决在读Access的记录时,以及不必循环插入SQL数据库,Insus.NET 把Access的最数据放在DataTable,并实现直接一次传完入数据库。

因此在SQL server创建一个表数据类型:

MS access 数据定时导入MS SQL Server View Code
CREATE TYPE [dbo].[udt_PrintLog] AS TABLE

    (

    [ID] INT NOT NULL,

    --other field list here

    )

GO


接下来,是创建一个存储过程:

MS access 数据定时导入MS SQL Server View Code
CREATE PROCEDURE [dbo].[usp_PrintLog_Import]

(

    @PrintLogCollection [dbo].[udt_PrintLog] READONLY

)

AS

INSERT INTO [dbo].[PrintLog] ([ID],...)

    SELECT pc.[ID],... FROM @PrintLogCollection AS pc;

GO


看到否,存储过程的参数的数据类型就是刚才定义好的数据类型。

 然后,再写一个存储过程,是返回SQL数据库表的最大记录ID,如果是空的话,它返回是0。这个值是为读取access的最新记录。

 

 下面是控制台应用程序,写一个类别,是处理SQL的存储过程的:

MS access 数据定时导入MS SQL Server PrintLog
using System;

using System.Collections.Generic;

using System.Data;

using System.Text;



namespace Insus.NET

{

    class PrintLog

    {

        public DataTable _PrintLogCollection;



        public DataTable PrintLogCollection

        {

            get

            {

                return _PrintLogCollection;

            }

            set

            {

                _PrintLogCollection = value;

            }

        }  

        

        BusinessBase objBusinessBase = new BusinessBase();



        public int GetMaxID()

        {

           return Convert.ToInt32(objBusinessBase.ExecuteProcedureScalar("usp_PrintLog_GetMaxID"));   

        }



        public void ImportData()

        {

            Parameter[] parameter = { 

                                    new Parameter ("@PrintLogCollection",SqlDbType.Structured,-1,_PrintLogCollection),                                    

                                    };

            objBusinessBase.ExecuteProcedure("usp_PrintLog_Import", parameter);        

        }

    }

}


PrintLog类别有一个实例化的类别,这个类别,可以从Insus.NET的博客找到:http://www.cnblogs.com/insus/articles/1654653.html

BusinessBase objBusinessBase = new BusinessBase();


控制台主程序:

MS access 数据定时导入MS SQL Server View Code
using System;

using System.Collections.Generic;

using System.Data;

using System.Data.OleDb;

using System.Text;



namespace Insus.NET

{

    class Program

    {

        static void Main(string[] args)

        {

            PrintLog objPrintLog = new PrintLog();

            int maxID = objPrintLog.GetMaxID();



            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Environment.CurrentDirectory + "\\printmgt.mdb";

            string sql = "SELECT [porder],... FROM [printlog] WHERE [porder] > " + maxID + "";

            DataSet objDs = new DataSet();



            using (OleDbConnection oleDbConn = new OleDbConnection(strConn))

            using (OleDbCommand objCmd = new OleDbCommand(sql, oleDbConn))

            using (OleDbDataAdapter objDa = new OleDbDataAdapter(objCmd))

            {

                objDa.Fill(objDs);

            }



            objPrintLog.PrintLogCollection = objDs.Tables[0];

            objPrintLog.ImportData();

        }

    }

}

System.Environment.CurrentDirectory这个主要是为了获取控制如应用程序所在目录位置,这样不管数据在那一个位置,只要把这个控制台应用程序可执行文件放在同Access的数据库同一目录即可。

  

你可能感兴趣的:(SQL Server)