///
/// 为包添加配置文件。
///
protected void SavePackage()
{
//新建dts执行程序
dts.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
//新建一个包
dts.Package pack = new Microsoft.SqlServer.Dts.Runtime.Package();
try
{
//加载一个存在的包
string PackagePath = Server.MapPath("") + "//Package_FilingBackUp.dtsx";
pack = app.LoadPackage(PackagePath, null);//加载包
if (pack.Configurations.Contains("DTSConn")) pack.Configurations.Remove("DTSConn");
//动态设置包的配置文件路径
pack.EnableConfigurations = true;//启用包配置
Microsoft.SqlServer.Dts.Runtime.Configuration conf = pack.Configurations.Add();//新建一个配置
conf.ConfigurationString = Server.MapPath("") + "//DtsConfig.dtsConfig";//设置配置文件的值为已存在的配置文件xml的路径
conf.ConfigurationType = dts.DTSConfigurationType.ConfigFile;//设置配置文件读取方式为xml文件
conf.PackagePath = PackagePath;//设置被配置的SSIS包路径。
conf.Name = "DTSConn";//设置此包的名称。
//保存SSIS
app.SaveToXml(PackagePath, pack, null);//最关键的一步
}
catch (Exception ex)
{
}
}
//运行DTS包
///
/// 运行DTS包
///
///
保存备份文件的路径
///
备份时间
///
备份文件文件名
private void Excutepackage(string path,string BeginTime,string EndTime, string Fname)
{
//新建dts执行程序
dts.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
//新建一个包
dts.Package pack = new Microsoft.SqlServer.Dts.Runtime.Package();
try
{
//加载一个存在的包
string PackagePath = Server.MapPath("") + "//Package_FilingBackUp.dtsx";
pack = app.LoadPackage(PackagePath, null);//加载包
//给变量赋值
pack.Variables["BeginTime"].Value = BeginTime;
pack.Variables["EndTime"].Value = EndTime;
pack.Variables["BakFilesPath"].Value = path;
//执行包
dts.DTSExecResult result = pack.Execute();
if (result.Equals(dts.DTSExecResult.Success))
{
try
{
//添加备份日志记录
BackupHisManager BackMan = new BackupHisManager();
BackupHis ent = (BackupHis)Weith.Toolkit.Reflection.TypeAccessor.CreateInstance(typeof(BackupHis));
ent.CreateTime = System.DateTime.Now;
ent.MediumNo = Fname;
ent.Creator = Global.GetUserID();
ent.MediumType = 0;
ent.DataFilePath = path;
BackMan.BackupHisAdd(ent);
Callbackresoult = "ClearData$" + BeginTime + "|" + EndTime + "|备份成功
备份时间:" + pack.Variables["BeginTime"].Value + "年到" + pack.Variables["EndTime"].Value + "年
备份路径:" + path + "
第三步:清除库中被备份的记录,请稍等...";
}
catch(Exception ex)
{
Callbackresoult = "wrong$备份失败:" + ex.Message;
}
}
//捕捉错误
string message = "";
if (result.Equals(dts.DTSExecResult.Failure))
{
for (int i = 0; i < pack.Errors.Count; i++)
{
message += pack.Errors[i].Description +"
";
}
Callbackresoult = "wrong$备份失败:" + message;
}
}
catch(Exception ex)
{
Callbackresoult = "wrong$备份失败:" + ex.Message;
}
}