sql 2008的SSIS应用方法: 将多个一定格式的文本文件批量导入到数据库

        private bool ExecutePackage(string sSourceFileName)
        {
            bool bResult = false;
            string srcpath = System.Windows.Forms.Application.StartupPath.Replace(@"\bin\Debug", "") + @"\Resources\NewPackage_20110526_02.dtsx";

            string filepath = System.Windows.Forms.Application.StartupPath + "\\Resources\\NewPackage_20110526_02.dtsx";
            if (!System.IO.File.Exists(filepath))
            {
                if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\Resources"))
                {
                    System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + "\\Resources");
                }
                System.IO.File.Copy(srcpath, filepath);
            }

            Microsoft.SqlServer.Dts.Runtime.Application application = new Microsoft.SqlServer.Dts.Runtime.Application();
            Package package = application.LoadPackage(filepath, null, true);

            package.Connections["SourceConnectionFlatFile"].ConnectionString= sSourceFileName;
            string sTargetConnection = string.Format(@"Data Source={0};User ID={1};Password={2};Initial Catalog={3};Provider=SQLNCLI10;Auto Translate=false;", sqlHelper.ImportTargetDataSource,sqlHelper.ImportTargetDBUid,sqlHelper.ImportTargetDBPwd,sqlHelper.ImportTargetDBName);
            package.Connections["DestinationConnectionOLEDB"].ConnectionString = sTargetConnection; 
            DTSExecResult result = package.Execute();
            if (result.Equals(DTSExecResult.Success))
            {
                bResult = true;
                //Common.DisplayMsg(this.Text, "成功导入数据");
            }
            else
            {
                string sError = string.Empty;
                for (int i = 0; i < package.Errors.Count; i++)
                {
                    sError +="(" + (i+1).ToString() + ")." + package.Errors[i].Description + Convert.ToChar(Keys.Return);
                }

                if (sError.Length > 1) sError = sError.TrimEnd(Convert.ToChar(Keys.Return));
                Common.DisplayMsg(this.Text, "导入失败:\r\n" + sError);
            }

            Common.DeleteFilesFromDisk(sSourceFileName);
            return bResult;
        }

 提示:

1,   sqlHelper.ImportTargetDataSource,sqlHelper.ImportTargetDBUid,sqlHelper.ImportTargetDBPwd,sqlHelper.ImportTargetDBName:这些设置连接你数据库的相关信息就OK了

2, 需要引用这2个命名空间,具体需要的DLL可以从网上百度到

         using Microsoft.SqlServer.Dts.Runtime;
         using Microsoft.SqlServer.Dts.Tasks.ExecutePackageTask;

3, 这里的 NewPackage_20110526_02.dtsx 是sql 2008下的包文件

        选中数据库--> 右键--> 任务--> 导入数据-->选择数据源为:平面数据源

        选中文件-->指定分割符-->绑定 源数据与目标数据的列 --> 最终生成一个.dtsx的包文件

4, 若有多个文本文件,在for循环中调用

         ExecutePackage(txtFilePath) 就OK了

5, 这种解决方案 一般适用于数据库字段 和 文本文件格式已经定型的情况。

    如果数据库字段,文本文件的列发生改变,那么 这个 .dtsx 重新生成一下就OK了。

    按上述操作完成后,用户可以随心所欲的选择 1个或多个 内容具有同样格式的文本文件,进行批量导入。

    我操作过10多个平均每个约1.5MB的文本文件(有10个字段列), 导入到sql2008的表中,数据大约有200W条,用时45秒左右。

    这个效率, 用sql语句简单的 for循环 200W次,可能没法达到。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值