SQL: SSIS的配置文件设置以及ssis包在web中的调用

  1.   private string SSISMatoMc()
  2.     {
  3.         ExecPackage ep = new ExecPackage();
  4.         ep.SavePackage("Pmatomc.dtsx""Cmatomc.dtsConfig");
  5.         return ep.Excutepackage("Pmatomc.dtsx""Cmatomc.dtsConfig");
  6.     }
ssis 包Pmatomc.dtsx 作用是把mA数据库中的表同步到mC数据库中
这样在配置文件Cmatomc.dtsConfig中至少要包括以下几个参数:数据库名称、连接字符串、连接密码。这样才能在不同的环境中使用(当然你还可以添加需要同步的表名到参数里)
  1. <?xml version="1.0"?>
  2. <DTSConfiguration>
  3.   <DTSConfigurationHeading>
  4.     <DTSConfigurationFileInfo GeneratedBy="computername" GeneratedFromPackageName="Pmatomc" GeneratedFromPackageID="{92A86513-2B8F-43D7-BDF2-1A2E3060D0ED}" GeneratedDate="2008-11-4 15:51:03"/>
  5.   </DTSConfigurationHeading>
  6.   
  7.   <!--mA configuration is below-->
  8.   <Configuration ConfiguredType="Property" Path="/Package/Transfer SQL Server Objects Task.Properties[SourceDatabase]" ValueType="String">
  9.     <ConfiguredValue>PRD_PPP_MA</ConfiguredValue>
  10.   </Configuration>
  11.   <Configuration ConfiguredType="Property" Path="/Package.Connections[localhost.sa].Properties[ConnectionString]" ValueType="String">
  12.     <ConfiguredValue>SqlServerName=localhost;UseWindowsAuthentication=False;UserName=sa;</ConfiguredValue>
  13.   </Configuration>
  14.   <Configuration ConfiguredType="Property" Path="/Package.Connections[localhost.sa].Properties[Password]" ValueType="String">
  15.     <ConfiguredValue>sa</ConfiguredValue>
  16.   </Configuration>
  17.   <!--mC configuration is below-->
  18.   <Configuration ConfiguredType="Property" Path="/Package/Transfer SQL Server Objects Task.Properties[DestinationDatabase]" ValueType="String">
  19.     <ConfiguredValue>PRD_PPP_MC</ConfiguredValue>
  20.   </Configuration>
  21.   <Configuration ConfiguredType="Property" Path="/Package.Connections[CNPRDWEBS02/CNDEV01.sa].Properties[ConnectionString]" ValueType="String">
  22.     <ConfiguredValue>SqlServerName=CNPRDWEBS02/CNDEV01;UseWindowsAuthentication=False;UserName=sa;</ConfiguredValue>
  23.   </Configuration>
  24.   <Configuration ConfiguredType="Property" Path="/Package.Connections[CNPRDWEBS02/CNDEV01.sa].Properties[Password]" ValueType="String">
  25.     <ConfiguredValue>sa</ConfiguredValue>
  26.   </Configuration>
  27. </DTSConfiguration>
调用类
  1. /// <summary>
  2. /// Summary description for ExecPackage
  3. /// </summary>
  4. public class ExecPackage
  5. {
  6.     /// <summary>
  7.     /// 为包添加配置文件
  8.     /// </summary>
  9.     /// <param name="PackageName"></param>
  10.     /// <param name="DtsxName"></param>
  11.     /// <returns></returns>
  12.     public bool SavePackage(string PackageName,string DtsxName)
  13.     {
  14.         //新建dts执行程序
  15.         Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
  16.         //新建一个包
  17.         Package pack = new Package();
  18.         try
  19.         {
  20.             string PackagePath = System.Web.HttpContext.Current.Server.MapPath("") +"//"+PackageName;
  21.             pack = app.LoadPackage(PackagePath, null);//加载包
  22.             if (pack.Configurations.Contains("DTSConn")) pack.Configurations.Remove("DTSConn");
  23.             //动态设置包的配置文件路径
  24.             pack.EnableConfigurations = true;                                                          //启用包配置
  25.             Microsoft.SqlServer.Dts.Runtime.Configuration conf = pack.Configurations.Add();            //新建一个配置
  26.             conf.ConfigurationString = System.Web.HttpContext.Current.Server.MapPath("") +"//"+ DtsxName; //设置配置文件的值为已存在的配置文件xml的路径
  27.             conf.ConfigurationType = DTSConfigurationType.ConfigFile;                                  //设置配置文件读取方式为xml文件
  28.             conf.PackagePath = PackagePath;                                                            //设置被配置的SSIS包路径。
  29.             conf.Name = "DTSConn";                                                                     //设置此包的名称。
  30.             app.SaveToXml(PackagePath, pack, null);                                                    //保存SSIS,最关键的一步
  31.             return true;
  32.         }
  33.         catch 
  34.         {
  35.             return false;
  36.         }
  37.     }
  38.     //运行DTS包
  39.     /// <summary>
  40.     /// 运行DTS包
  41.     /// </summary>
  42.     /// <param name="path">保存备份文件的路径</param>
  43.     /// <param name="CreateTime">备份时间</param>
  44.     /// <param name="Fname">备份文件文件名</param>
  45.     public string Excutepackage(string PackageName, string DtsxName)
  46.     {
  47.         //新建dts执行程序
  48.         Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
  49.         //新建一个包
  50.         Package pack = new Package();
  51.         string message = "";
  52.         try
  53.         {
  54.             //加载一个存在的包
  55.             string PackagePath = System.Web.HttpContext.Current.Server.MapPath("") + "//" + PackageName;//"//Package_mAtomC.dtsx";
  56.             pack = app.LoadPackage(PackagePath, null);//加载包
  57.             //给变量赋值
  58.             //pack.Variables["BeginTime"].Value = BeginTime;
  59.             //pack.Variables["EndTime"].Value = EndTime;
  60.             //pack.Variables["BakFilesPath"].Value = path;
  61.             //执行包
  62.             DTSExecResult result = pack.Execute();
  63.            
  64.             //捕捉错误         
  65.             if (result.Equals(DTSExecResult.Failure))
  66.             {
  67.                 for (int i = 0; i < pack.Errors.Count; i++)
  68.                 {
  69.                     message += pack.Errors[i].Description;
  70.                 }
  71.             }
  72.             else message = result.ToString();
  73.         }
  74.         catch (Exception ex)
  75.         {
  76.             message = ex.ToString();
  77.         }
  78.         return message;
  79.     }
  80. }
web页面中调用:
  1.   private string SSISMatoMc()
  2.     {
  3.         ExecPackage ep = new ExecPackage();
  4.         ep.SavePackage("Pmatomc.dtsx""Cmatomc.dtsConfig");
  5.         return ep.Excutepackage("Pmatomc.dtsx""Cmatomc.dtsConfig");
  6.     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值