这是一个典型的C#代码,除了用using关键词导入.Net命名空间的类外,数字平台公司也开发了很多专用类,可以很方便二次开发人员直接调用。批处理程序通过以下几个过程(可选的)实现功能:
OnInitial——批处理过程初始化阶段执行的代码。
OnBegin——批处理过程开始阶段执行的代码。
Inputing——数据导入数据库阶段执行的代码(与Outputing阶段互斥)。
Outputing——数据库中导出数据阶段执行的代码(与Inputing阶段互斥)。
OnEnd——批处理过程结束阶段的代码。
具体专用类的调用方式见相关手册,以下是“一边转入一边查重”的完整示例代码:
using System;
using System.Windows.Forms;
using System.IO;
using System.Text;
using DigitalPlatform.MarcDom;
using DigitalPlatform.Library;
using DigitalPlatform.Marc;
using DigitalPlatform.Statis;
using dp2Batch;
public class MyBatch : Batch
{
DupDlg dupdlg = null;
int m_nDupCount = 0;
Encoding OutputEncoding = Encoding.UTF8;
Stream DupOutputFile = null;
public string Title = "";
public override void OnInitial(object sender, BatchEventArgs e)
{
// MessageBox.Show(this.MainForm, "OnInitial");
}
public override void OnBegin(object sender, BatchEventArgs e)
{
MessageBox.Show(this.MainForm, "OnBegin");
if (dupdlg == null)
{
dupdlg = new DupDlg();
dupdlg.Initial(this.SearchPanel,
this.SearchPanel.ServerUrl,
false);
dupdlg.ProjectName = "";
dupdlg.Closed -= new
EventHandler(dupdlg_Closed);
dupdlg.Closed += new
EventHandler(dupdlg_Closed);
dupdlg.LoadBrowse = LoadBrowse.Dup; // 只装
载超过阈值的浏览记录
dupdlg.Show();
DialogResult ret = dupdlg.FindProjectName();
if (ret != DialogResult.OK)
{
e.Continue = ContinueType.SkipAll;
return;
}
}
// 查重发现重复的MARC输出文件
OpenMarcFileDlg dlg = new OpenMarcFileDlg();
dlg.Text = "请指定经查重发现重复后输出的 ISO2709 文件";
dlg.FileName = "output.mrc";
this.ap.LinkFormState(dlg, "dupmarc_dlg_state");
dlg.ShowDialog(this.MainForm);
this.ap.UnlinkFormState(dlg);
if (dlg.DialogResult != DialogResult.OK)
{
DupOutputFile = null;
}
else
{
DupOutputFile = File.Create(dlg.FileName);
}
}
public override void Inputing(object sender, BatchEventArgs e)
{
string strError = "";
// MessageBox.Show(this.MainForm, this.Title);
if (dupdlg == null)
{
e.Continue = ContinueType.SkipAll;
return;
}
try {
dupdlg.Activate();
dupdlg.ServerUrl = this.ServerUrl;
dupdlg.RecordFullPath = this.RecFullPath;
dupdlg.Record = this.XmlRecord;
int nRet = dupdlg.DoSearch(out strError);
if (nRet == -1)
{
MessageBox.Show(this.MainForm, strError);
return;
}
if (dupdlg.DupPaths.Length > 0)
{
m_nDupCount ++;
this.SkipInput = true;
if (DupOutputFile != null)
{
// 遇到重复,向外写ISO2709文件
byte [] baResult = null;
nRet = MarcUtil.CvtJineiToISO2709(
this.MarcRecord,
this.MarcSyntax,
OutputEncoding,
out baResult,
out strError);
if (nRet == -1)
{
MessageBox.Show(this.MainForm,
strError);
return;
}
DupOutputFile.Write(baResult, 0,
baResult.Length);
}
}
}
catch (Exception ex)
{
MessageBox.Show(this.MainForm, ex.Message);
e.Continue = ContinueType.SkipAll;
return;
}
}
public override void OnEnd(object sender, BatchEventArgs e)
{
if (DupOutputFile != null)
{
DupOutputFile.Close();
}
MessageBox.Show(this.MainForm, "OnEnd");
}
private void dupdlg_Closed(object sender, EventArgs e)
{
dupdlg = null;
}
}