private void 导入TSLToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("使用方法:将tpr报表保存成txt文本文档后,导入即可!"); string filename = ""; try { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt|TPR报表文件(*.tpr)|*.tpr"; string path = Application.StartupPath; if (!Directory.Exists(path + "\\Tpr")) { Directory.CreateDirectory(path + "\\Tpr"); } dlg.InitialDirectory = Application.StartupPath + "\\Tpr"; if (dlg.ShowDialog() == DialogResult.OK) { filename = dlg.FileName; MessageBox.Show (GlobalApi.getOffice(dlg.FileName) + "\n" + GlobalApi.getDateOfSale(dlg.FileName) + "\n" + GlobalApi.getTotalTicket(dlg.FileName) + "张电子客票" + "\n" + GlobalApi.getNORMALTICKETS(dlg.FileName) ); GlobalApi.importAllLineFromTpr(dlg.FileName); MessageBox.Show("导入完成" + filename); } } catch (Exception ex) { MessageBox.Show(ex.Message + filename); GlobalApi.appenderrormessage(ex.Message + "导入失败" + filename); } }
public static void importOneLineFromTpr(string line, string filename) { try { if (line.Trim().Length < 63) { return; } if (line[0] > '9' || line[0] < '0') { return; } string str = line.Trim().ToUpper(); string tktnumber = str.Substring(0, 14).Trim(); string orig_dest = str.Substring(18, 9).Trim(); string collection = str.Substring(27, 10).Trim(); string tax = str.Substring(37, 10).Trim(); string comm = str.Substring(47, 11).Trim();//没导入 string pnr = str.Substring(58, 6).Trim(); if (pnr.Length != 5) { pnr = str.Substring(58); string[] p = pnr.Split(' '); for (int i = 0; i < p.Length; i++) { if (p[i].Trim().Length == 5) { pnr = p[i].Trim(); break; } } } string dateofsale = GlobalApi.getDateOfSale(filename); //判断票号是否已经入库 string number = tktnumber.Substring(4); { string cmdString = string.Format("select * from etickets where TKTNUMBER='{0}'", number); OleDbCommand cmd = new OleDbCommand(cmdString, GlobalVar.cn); DataTable dt = new DataTable(); OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); adapter.Fill(dt); if (dt.Rows.Count != 1) { throw new Exception(number + "票号未入库"); } } //更新数据库 { FormMain.LABEL.Text = string.Format("正在导入tpr报表:票号{0}", tktnumber); Application.DoEvents(); string cmdString = "update etickets set "; if (tktnumber.Length == 14) { cmdString += string.Format("[E-TKT-NUMBER]='{0}'", tktnumber); } if (orig_dest.Contains("REFUND")) { cmdString += string.Format(", [ORIG-DEST]='{0}',[TKT-FLAG]='4'", orig_dest);//cmdString += ", [TKT-FLAG]='4'"; } else if (orig_dest.Contains("VOID")) { cmdString += string.Format(", [ORIG-DEST]='{0}',[TKT-FLAG]='2'", orig_dest);//cmdString += ", [TKT-FLAG]='2'"; } else if (orig_dest.Length == 7) { cmdString += string.Format(", [ORIG-DEST]='{0}',[TKT-FLAG]='1'", orig_dest);//cmdString += ", [TKT-FLAG]='1'"; } if (dateofsale != "") { cmdString += string.Format(", DATEOFSALE='{0}'", dateofsale); } if (collection.Length != 0 && collection.IndexOf('.') > 0) { cmdString += string.Format(", COLLECTION={0}", int.Parse(collection.Split('.')[0])); } if (tax.Length != 0 && tax.IndexOf('.') > 0) { cmdString += string.Format(", TAXS={0}", int.Parse(tax.Split('.')[0])); } if (pnr.Length == 5) { cmdString += string.Format(", PNR='{0}'", pnr); } cmdString += string.Format(", IMPORTCOUNT=IMPORTCOUNT+1"); cmdString += string.Format(" where TKTNUMBER='{0}'", number); OleDbCommand cmd = new OleDbCommand(cmdString, GlobalVar.cn); cmd.ExecuteNonQuery(); GlobalVar.countStat++; } } catch (Exception ex) { //MessageBox.Show(ex.Message); GlobalApi.appenderrormessage("importOneLineFromTpr: " + ex.Message); } }