using Lskj.Core; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Lserp_FileToDB { public partial class Form1 : Form { #region 变量 private bool NewInitSuccess = false; private Int64 NewTxtMaxLength = 10000; IniFiles ini = new IniFiles(Application.StartupPath + @"\MyConfig.INI"); List id = new List(); bool fg = false; bool falg = false; string str = ""; string len = ""; string time = ""; #endregion public Form1() { InitializeComponent(); } /// /// 向数据库添加数据 /// public void start() { if (!NewInitSuccess) return; falg = false; this.timer1.Enabled = false; RefreshLogUIThread(DateTime.Now + " 开始更新数据.\r\n"); string[] files = File.ReadAllLines(str, Encoding.Default); foreach (string item in files) { id.Add(item.Substring(0,int.Parse(len))); } for(int i=0;i 0) { continue; } else { SqlHelper.ExecuteNonQuery(string.Format("Insert into P_PrintRecordTab VALUES ('{0}','{1}')", id[i], DateTime.Now.ToString())); RefreshLogUIThread(DateTime.Now + string.Format(" {0} 数据添加成功.\r\n", id[i])); } } falg = true; RefreshLogUIThread(DateTime.Now + " 本轮数据发送完成.\r\n"); RefreshLogUIThread("--------------------分割线------------------------\r\n"); } private void timer1_Tick(object sender, EventArgs e) { id.Clear(); timer1.Enabled = false; start(); timer1.Enabled = true; } private void btn_updata_Click(object sender, EventArgs e) { add a = new add(); a.ShowDialog(); MessageBox.Show("更新成功重新初始化!"); Initialization(); } /// /// 项目开始执行 /// /// /// private void Form1_Load(object sender, EventArgs e) { this.Initialization(); // 初始化文件检测平台 // 创建后台线程 Thread backThread = new Thread(start); backThread.IsBackground = true; backThread.Start(); } /// /// 初始化文件检测平台 /// private void Initialization() { try { RefreshLog(DateTime.Now + " 正在初始化平台.\r\n"); if (DBConfig.Instance.CreateConnection()) { DataTable DT = SqlHelper.ExecuteDataTable("SELECT Name FROM SysObjects Where XType='U' and Name='P_PrintRecordTab' ORDER BY Name"); bool flag = DT.Rows.Count > 0; if (flag) { //获取ini配置的值 str = ini.IniReadValue("文件检测", "Path"); len = ini.IniReadValue("文件检测", "Len"); time = ini.IniReadValue("文件检测", "Time"); if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(len) || string.IsNullOrEmpty(time)) { RefreshLog(DateTime.Now + " 初始化平台失败.\r\n" + "原因:配置文件不全请重新配置."); MessageBox.Show(" 初始化平台失败.\r\n" + "原因:配置文件不全请重新配置."); add a = new add(); a.ShowDialog(); Initialization(); } else { NewInitSuccess = true; RefreshLog(DateTime.Now + " 初始化平台成功.\r\n"); this.timer1.Interval = int.Parse(time) * 1000; this.timer1.Start(); this.timer2.Interval = 100; this.timer2.Start(); } } else { MessageBox.Show("此数据库内无此表!请配置好注册表后重新打开!"); this.Close(); return; } } else { RefreshLog(DateTime.Now + " 初始化平台失败.\r\n"); } } catch (Exception ex) { RefreshLog(DateTime.Now + string.Format(" 初始化失败.原因:{0}.\r\n", ex.Message)); } } /// /// 后台线程刷新数据 /// /// public void RefreshLogUIThread(string msg) { Invoke((EventHandler)delegate { txtLog.Text += msg; txtLog.Select(txtLog.TextLength, 0); txtLog.ScrollToCaret(); }); } /// /// 主线程刷新数据 /// /// public void RefreshLog(string msg) { txtLog.Text += msg; txtLog.Select(txtLog.TextLength, 0); txtLog.ScrollToCaret(); } /// /// 日志 /// /// /// private void txtLog_TextChanged(object sender, EventArgs e) { if (this.txtLog.Text.Length > NewTxtMaxLength) { Invoke((EventHandler)delegate { txtLog.Text = ""; txtLog.Select(txtLog.TextLength, 0); txtLog.ScrollToCaret(); }); } } /// /// 暂停 /// /// /// private void btn_stop_Click(object sender, EventArgs e) { timer1.Enabled = false; } /// /// 继续 /// /// /// private void btn_start_Click(object sender, EventArgs e) { timer1.Enabled = true; } /// /// 关闭软件重置ini文件 /// /// /// private void Form1_FormClosing(object sender, FormClosingEventArgs e) { ini.IniWriteValue("文件检测", null, null); } /// /// 控制timer1 /// /// /// private void timer2_Tick(object sender, EventArgs e) { if (falg) { this.timer1.Enabled = true; } } } }