C#实现图书馆程序导入ISO-2709格式(MARC)功能

1.导入

/// <summary>
/// 导入ISO2709
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 导入ISO2709ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "ISO2709文件|*.ISO";
ofd.Multiselect = false;
ofd.Title = "请选择要导入的ISO2709文件.";
if (ofd.ShowDialog() != DialogResult.OK)
{
return;
}
if (MessageBox.Show("要将“" + ofd.FileName + "”装入“" + toolStripcmb_SMK.Text + "”的待选书库吗?", "上装确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
StreamReader objReader = new StreamReader(ofd.FileName, System.Text.Encoding.Default);
string sLine = "";
ArrayList _ArrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
_ArrText.Add(sLine);
}
objReader.Close();

DBUtility.SQuery sq;
foreach (string marc in _ArrText)
{
SortedList<string, string> Li = GetData(marc);
//要插入的marc
string insert_marc = marc.Substring(5, 4) + (char)30;//头标区
for (int i = 0; i < Li.Count; i++)
{
//拼接要插入的marc字段
insert_marc += Li.Keys[i] + Li.Values[i] + (char)30;//不能去掉(char)31,去掉(char)30添加(char)30(标识3+指2+字段内容)
}
sq = DBUtility.SQuery.Create(
@"INSERT INTO 待采书库 (馆键码,标准编码,题名,责任者,版次,出版者,出版年,单价,MARC,操作员,入库日期,库键码,征订号) VALUES
(@馆键码,@标准编码,@题名,@责任者,@版次,@出版者,@出版年,@单价,@MARC,@操作员,getdate(),@库键码,@征订号)");
//解析marc
sq.Params.Add("@馆键码", SystemState.GJM);
sq.Params.Add("@标准编码", Li.Keys.Contains("010") ? GetMarcValue(Li["010"], "标准编码") : "");
sq.Params.Add("@题名", Li.Keys.Contains("200") ? GetMarcValue(Li["200"], "题名") : "");
sq.Params.Add("@责任者", Li.Keys.Contains("200") ? GetMarcValue(Li["200"], "责任者") : "");
sq.Params.Add("@版次", Li.Keys.Contains("205") ? GetMarcValue(Li["205"], "版次") : "");
sq.Params.Add("@出版者", Li.Keys.Contains("210") ? GetMarcValue(Li["210"], "出版者") : "");
sq.Params.Add("@出版年", Li.Keys.Contains("210") ? GetMarcValue(Li["210"], "出版年") : "");
sq.Params.Add("@单价", Li.Keys.Contains("010") ? GetMarcValue(Li["010"], "单价") : "");
sq.Params.Add("@marc", insert_marc);
sq.Params.Add("@操作员", Common.SystemState.UserID);
sq.Params.Add("@库键码", toolStripcmb_SMK.ComboBox.SelectedValue);
sq.Params.Add("@征订号", Li.Keys.Contains("092") ? GetMarcValue(Li["092"], "征订号") : "");
sq.ExecuteNonQuery();
//success += sq.ExecuteNonQuery() > 0 ? 1 : 0;
}
MessageBox.Show("数据上装完毕", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//刷新
toolStripbtn_query_Click(sender, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

//解析指定MARC数据,返回SortedList<>
private SortedList<string, string> GetData(string MarcText)
{
int MarcTextLength = Convert.ToInt32(MarcText.Substring(0, 5));//MARC记录文本总长度,00931
int _ConPos = Convert.ToInt32(MarcText.Substring(12, 5));//数据字段区起始地址,00253

int _Count = (_ConPos - 24) / 12;//字段个数,19
string _Address = MarcText.Substring(24, _ConPos - 24 - 1);//地址目次区字符串
string _Contents = MarcText.Substring(_ConPos);//数据字段区字符串

//---------开始解析字段数据

string[] _Keys = new string[_Count];//字段编号
string[] _Values = new string[_Count];//字段值
string[] Values = _Contents.Split((char)30);//用(char)30分割数据字段区字符串,用于给后面的_Keys对应字段内容
for (int i = 0; i < _Count; i++)
{
string _Num = _Address.Substring(i * 12, 3);//字段编号
int _Len = int.Parse(_Address.Substring(i * 12 + 3, 4));//字段对应的数据区长度
int _Pos = int.Parse(_Address.Substring(i * 12 + 7, 5));//字段对应的数据区开始位置

_Keys[i] = _Address.Substring(i * 12, 3);//填充字段编号

_Values[i] = Values[i];//填充字段值

}

SortedList<string, string> _SL = new SortedList<string, string>();
for (int i = 0; i < _Count; i++)
{
if (_SL.ContainsKey(_Keys[i]))
{
_SL[_Keys[i]] += (char)31 + _Values[i];//如果存在2个701则用(char)31分开
}
else
{
_SL.Add(_Keys[i], _Values[i]);
}
}

return _SL;
}

/// <summary>
/// 获取正题名
/// </summary>
private string GetMarcValue(string str, string item)
{
string[] subArray = str.Substring(str.IndexOf((char)31)).Trim().Split(new char[] { (char)31 }, StringSplitOptions.RemoveEmptyEntries);

foreach (string subStr in subArray)
{
switch (item)
{
case "题名":
case "语种":
case "出版地":
case "标准编码":
if (subStr.Substring(0, 1) == "a")
{
return subStr.Substring(1);
}
break;
case "题名缩写":
if (subStr.Substring(0, 1) == "9")
{
return subStr.Substring(1);
}
break;
case "责任者":
if (subStr.Substring(0, 1) == "f")
{
return subStr.Substring(1);
}
break;
case "出版者":
if (subStr.Substring(0, 1) == "c")
{
return subStr.Substring(1);
}
break;
case "版次":
if (subStr.Substring(0, 1) == "a")
{
return subStr.Substring(1);
}
break;
case "单价":
if (subStr.Substring(0, 1) == "d")
{
return subStr.Substring(1);
}
break;
case "出版年":
if (subStr.Substring(0, 1) == "d")
{
return subStr.Substring(1);
}
break;
case "索书号":
if (subStr.Substring(0, 1) == "d")
{
string val = "";
val = subStr.Substring(1);
foreach (string subStr2 in subArray)
{
if (subStr.Substring(0, 1) == "e")
{
val += "/" + subStr2.Substring(1);
break;
}
}
return val;
}
break;
case "征订号":
if (subStr.Substring(0, 1) == "b")
{
return subStr.Substring(1);
}
break;
}

}
return "";
}

原文地址:https://www.cnblogs.com/luoxiaozhao/p/5135036.html