ab56a9bcf7
SVN-Revision: r240
237 lines
7.8 KiB
C#
237 lines
7.8 KiB
C#
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<string> id = new List<string>();
|
|
bool fg = false;
|
|
bool falg = false;
|
|
string str = "";
|
|
string len = "";
|
|
string time = "";
|
|
#endregion
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 向数据库添加数据
|
|
/// </summary>
|
|
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<files.Length;i++){
|
|
int count = (Int32)SqlHelper.ExecuteScalar(string.Format("select count(*) from P_PrintRecordTab where ProductID='{0}'", id[i]));
|
|
if (count > 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();
|
|
}
|
|
/// <summary>
|
|
/// 项目开始执行
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
{
|
|
this.Initialization(); // 初始化文件检测平台
|
|
// 创建后台线程
|
|
Thread backThread = new Thread(start);
|
|
backThread.IsBackground = true;
|
|
backThread.Start();
|
|
}
|
|
/// <summary>
|
|
/// 初始化文件检测平台
|
|
/// </summary>
|
|
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));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 后台线程刷新数据
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
public void RefreshLogUIThread(string msg)
|
|
{
|
|
Invoke((EventHandler)delegate
|
|
{
|
|
txtLog.Text += msg;
|
|
txtLog.Select(txtLog.TextLength, 0);
|
|
txtLog.ScrollToCaret();
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 主线程刷新数据
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
public void RefreshLog(string msg)
|
|
{
|
|
txtLog.Text += msg;
|
|
txtLog.Select(txtLog.TextLength, 0);
|
|
txtLog.ScrollToCaret();
|
|
}
|
|
/// <summary>
|
|
/// 日志
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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();
|
|
});
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 暂停
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btn_stop_Click(object sender, EventArgs e)
|
|
{
|
|
timer1.Enabled = false;
|
|
}
|
|
/// <summary>
|
|
/// 继续
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btn_start_Click(object sender, EventArgs e)
|
|
{
|
|
timer1.Enabled = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭软件重置ini文件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
ini.IniWriteValue("文件检测", null, null);
|
|
}
|
|
/// <summary>
|
|
/// 控制timer1
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void timer2_Tick(object sender, EventArgs e)
|
|
{
|
|
if (falg)
|
|
{
|
|
this.timer1.Enabled = true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|