using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;//启动SQL数据库
using System.IO;//启动文本读写
using System.Threading;//启动多线层
using System.Diagnostics;//启动网络共享访问连接
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string KeyValues1 = string.Empty;//储存查询关键字1
string KeyValues2 = string.Empty;//储存查询关键字2
string ReadDataSourcesPath = string.Empty;//读取数据源路径
List<string> SourcesTextName = new List<string>();//储存读取数据源文件名称
public bool ReadCfgKey(string CfgName)//读取配置
{
FileStream fs = new FileStream(CfgName, FileMode.Open);//打开配置文件
StreamReader sr = new StreamReader(fs);//导入读取流
try
{
string Temp = string.Empty;
while ((Temp = sr.ReadLine()) != null)
{
string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);//分离=字符串前后数据
if (Array[0].Trim() == "PATH")
ReadDataSourcesPath = Array[1].Trim();
else if (Array[0].Trim() == "Key1")
KeyValues1 = Array[1].Trim();
else if (Array[0].Trim() == "Key2")
KeyValues2 = Array[1].Trim();
Temp = string.Empty;
}
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sr.Close();//关闭读取流
fs.Close();//关闭文本流
}
return false;
}
public void SetItemProperty(string str,Label Key,Color BackKey1,Color ForeKey2)//设置读取配置成功后控件属性
{
Key.BackColor = BackKey1;//背景颜色设置
Key.ForeColor = ForeKey2;//字体颜色设置
Key.Text = str;//文本内容设置
}
public bool LinkServerShare(string Spath,string userName,string passWord)//连接服务器共享
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine=@"net use "+Spath+" /User:"+userName+" "+passWord;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while(!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
Flag = true;
else
{
MessageBox.Show(errormsg);
Flag = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
public bool ReadSourcesTextName(string SourcesPath,string str)//读取共享目录下指定“str”格式文件名称
{
bool Flay = false;
try
{
DirectoryInfo dinfo = new DirectoryInfo(SourcesPath);//实例化DirectoryInfo对象
FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();//获取指定目录的所有子目录及文件内型
foreach(FileSystemInfo fsinfo in fsinfos)
{
if(Path.GetExtension(fsinfo.Name)==str)
{
string Temp = string.Empty;
Temp = SourcesPath + @"\" + fsinfo.Name;
SourcesTextName.Add(Temp);
}
}
Flay = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
Flay = false;
}
return Flay;
}
//bool IsCfgNetOk = false;
private void Form1_Load(object sender, EventArgs e)
{
if((ReadCfgKey("config.ini"))==true)//判断配置文件是否读取成功
{
SetItemProperty("配置读取:->成功",label4,Color.Green,Color.Gold);
if(LinkServerShare(ReadDataSourcesPath,"admin","test")==true)//判断share是否连接成功
{
SetItemProperty("网络连接:->成功",label3,Color.Green,Color.Gold);
if (ReadSourcesTextName(ReadDataSourcesPath, @".txt") == true)//读取目录下所有数据文件名
{
//IsCfgNetOk = true;//确认网络与配置成功设置
button1.Enabled = true;
}
}
}
}
public void SetProgressInitial(ProgressBar Key,int MaxValues,int InitValues,Color Key1)
{
Key.Value = InitValues;//进程初始值
Key.Maximum = MaxValues;//最大值
Key.BackColor = Key1;//背景颜色
}
int MainProgressValues = 0;
public void CreateThread1()
{
//this.timer1.Enabled = true;
this.progressBar1.Value = MainProgressValues;//设置主进程进度
}
public void StartMainThread()
{
Control.CheckForIllegalCrossThreadCalls = false;
Thread td1 = new Thread(new ThreadStart(CreateThread1));//启动线层1
td1.IsBackground = true;
td1.Start();
}
public bool IsSnAndMadExists(string Key,string Key1)
{
bool Flay=false;
SqlConnection conn = new SqlConnection("server=.;database=E_TestData;uid=sa;pwd=system");//启动连接数据库
try
{
conn.Open();//打开连接
SqlCommand cmd = new SqlCommand("usp_SelectTDATA",conn);//启动命令行
cmd.CommandType = CommandType.StoredProcedure;//启动存储过程带参数功能
cmd.Parameters.Add("@SN",Key);//添加T-SQL存储过程参数1
cmd.Parameters.Add("Mac_Address",Key1);//添加T-SQL存储过程参数
cmd.Parameters.Add("@id",222);//添加T-SQL存储过程参数
cmd.Parameters["@id"].Direction = ParameterDirection.Output;//启动输出返回
cmd.ExecuteScalar();
if(((int) cmd.Parameters["@id"].Value)==0)
{
conn.Close();
Flay = true;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
Flay = false;
}
finally
{
conn.Close();
}
return Flay;
}
public void WriteLog(string Key)
{
string path = DateTime.Now.ToString("yyyyMMdd") + @"Data.log";
FileStream fs;
if(File.Exists(path))
{
fs = new FileStream(path,FileMode.Append,FileAccess.Write);//不创建文件写入
}
else
{
fs = new FileStream(path,FileMode.Create,FileAccess.Write);//创建文件写入
}
StreamWriter swMyfile = new StreamWriter(fs);//写入流
string Temp = string.Empty;
Temp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Temp = Temp + Key;
swMyfile.WriteLine(Temp);
swMyfile.Close();
fs.Close();
}
public void FullSqlData(List<string> Key,RichTextBox Key1)
{
string Temp = string.Empty;
this.progressBar2.Value = 0;
this.progressBar2.Maximum = Key.Count;//获取数据长度
int n = 1;
foreach(string str in Key)
{
string[] TestDataField = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
if((IsSnAndMadExists(TestDataField[0],TestDataField[1]))==false)
{
WriteLog(str);
}
else
{
try
{
SqlConnection conn = new SqlConnection("server=.;database=E_TestData;uid=sa;pwd=system");
conn.Open();
SqlCommand cmd = new SqlCommand("usp_InsertTDATA",conn);
cmd.CommandType = CommandType.StoredProcedure;//启动储存过程带参数
cmd.Parameters.Add("@SN",TestDataField[0].Trim());
cmd.Parameters.Add("@Mac_Address",TestDataField[1].Trim());
cmd.Parameters.Add("@State",TestDataField[2].Trim());
cmd.Parameters.Add("@Line",TestDataField[3].Trim());
cmd.Parameters.Add("@Station",TestDataField[4].Trim());
cmd.Parameters.Add("@JobNumber",TestDataField[5].Trim());
cmd.Parameters.Add("@Model",TestDataField[6].Trim());
cmd.Parameters.Add("@Date_Time",TestDataField[7].Trim());
cmd.Parameters.Add("@rs",1);
cmd.Parameters.Add("@rs1",1);
cmd.Parameters["@rs"].Direction = ParameterDirection.Output;
cmd.Parameters["@rs1"].Direction = ParameterDirection.Output;
cmd.ExecuteScalar();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
MessageBox.Show(dr[0].ToString());
dr.Close();
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
output(n, richTextBox1);
this.progressBar2.Value = n;
n++;
}
}
public void output(int Values,RichTextBox Key)
{
if (Values==0)
{
string temp = string.Empty;
temp = DateTime.Now.ToString("HH:mm:ss");
temp += " 进度条开始执行\n";
Key.AppendText(temp);
}
else
{
string temp = string.Empty;
temp = DateTime.Now.ToString("HH:mm:ss");
temp += " 进度进行中[" + MainProgressValues + " / " + Values + "]\n";
Key.AppendText(temp);
}
}
public void WriteReadText(string str)
{
string FileName = DateTime.Now.ToString("yyyyMMdd") + @"FileName.log";
FileStream fs;
if(File.Exists(FileName))
{
fs = new FileStream(FileName,FileMode.Append,FileAccess.Write);//不创建文件
}
else
{
fs = new FileStream(FileName,FileMode.Create,FileAccess.Write);//创建文件
}
StreamWriter sw = new StreamWriter(fs);
string temp = string.Empty;
temp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
temp += str;
sw.WriteLine(temp);
sw.Close();
fs.Close();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
SetProgressInitial(progressBar1, SourcesTextName.Count,0,Color.Green);//主进程设置
progressBar1.Value = 0;
progressBar1.Maximum = SourcesTextName.Count;
output(0,richTextBox1);
foreach(string str in SourcesTextName)
{
WriteReadText(str);//写入读取的文本
FileStream fs = new FileStream(str, FileMode.Open);//打开文本
StreamReader sr = new StreamReader(fs,Encoding.Default);//允许中文方式打开
List<string> Array = new List<string>();
string Temp = string.Empty;
while((Temp=sr.ReadLine())!=null)
{
string[] KeyValues = Temp.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);//截取,或空后面数据
string[] KeyValues8 = KeyValues[7].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);//截取,空后面数据
if (KeyValues8[0].Trim() == KeyValues1 || KeyValues8[0].Trim() == KeyValues2)
Array.Add(Temp);
Temp = string.Empty;
}
FullSqlData(Array, richTextBox1);
Array.RemoveRange(0,Array.Count);//清除所有项
fs.Close();
sr.Close();
progressBar1.Value = MainProgressValues;
MainProgressValues++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
CreateThread1();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;//启动SQL数据库
using System.IO;//启动文本读写
using System.Threading;//启动多线层
using System.Diagnostics;//启动网络共享访问连接
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string KeyValues1 = string.Empty;//储存查询关键字1
string KeyValues2 = string.Empty;//储存查询关键字2
string ReadDataSourcesPath = string.Empty;//读取数据源路径
List<string> SourcesTextName = new List<string>();//储存读取数据源文件名称
public bool ReadCfgKey(string CfgName)//读取配置
{
FileStream fs = new FileStream(CfgName, FileMode.Open);//打开配置文件
StreamReader sr = new StreamReader(fs);//导入读取流
try
{
string Temp = string.Empty;
while ((Temp = sr.ReadLine()) != null)
{
string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);//分离=字符串前后数据
if (Array[0].Trim() == "PATH")
ReadDataSourcesPath = Array[1].Trim();
else if (Array[0].Trim() == "Key1")
KeyValues1 = Array[1].Trim();
else if (Array[0].Trim() == "Key2")
KeyValues2 = Array[1].Trim();
Temp = string.Empty;
}
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sr.Close();//关闭读取流
fs.Close();//关闭文本流
}
return false;
}
public void SetItemProperty(string str,Label Key,Color BackKey1,Color ForeKey2)//设置读取配置成功后控件属性
{
Key.BackColor = BackKey1;//背景颜色设置
Key.ForeColor = ForeKey2;//字体颜色设置
Key.Text = str;//文本内容设置
}
public bool LinkServerShare(string Spath,string userName,string passWord)//连接服务器共享
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine=@"net use "+Spath+" /User:"+userName+" "+passWord;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while(!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
Flag = true;
else
{
MessageBox.Show(errormsg);
Flag = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
public bool ReadSourcesTextName(string SourcesPath,string str)//读取共享目录下指定“str”格式文件名称
{
bool Flay = false;
try
{
DirectoryInfo dinfo = new DirectoryInfo(SourcesPath);//实例化DirectoryInfo对象
FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();//获取指定目录的所有子目录及文件内型
foreach(FileSystemInfo fsinfo in fsinfos)
{
if(Path.GetExtension(fsinfo.Name)==str)
{
string Temp = string.Empty;
Temp = SourcesPath + @"\" + fsinfo.Name;
SourcesTextName.Add(Temp);
}
}
Flay = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
Flay = false;
}
return Flay;
}
//bool IsCfgNetOk = false;
private void Form1_Load(object sender, EventArgs e)
{
if((ReadCfgKey("config.ini"))==true)//判断配置文件是否读取成功
{
SetItemProperty("配置读取:->成功",label4,Color.Green,Color.Gold);
if(LinkServerShare(ReadDataSourcesPath,"admin","test")==true)//判断share是否连接成功
{
SetItemProperty("网络连接:->成功",label3,Color.Green,Color.Gold);
if (ReadSourcesTextName(ReadDataSourcesPath, @".txt") == true)//读取目录下所有数据文件名
{
//IsCfgNetOk = true;//确认网络与配置成功设置
button1.Enabled = true;
}
}
}
}
public void SetProgressInitial(ProgressBar Key,int MaxValues,int InitValues,Color Key1)
{
Key.Value = InitValues;//进程初始值
Key.Maximum = MaxValues;//最大值
Key.BackColor = Key1;//背景颜色
}
int MainProgressValues = 0;
public void CreateThread1()
{
//this.timer1.Enabled = true;
this.progressBar1.Value = MainProgressValues;//设置主进程进度
}
public void StartMainThread()
{
Control.CheckForIllegalCrossThreadCalls = false;
Thread td1 = new Thread(new ThreadStart(CreateThread1));//启动线层1
td1.IsBackground = true;
td1.Start();
}
public bool IsSnAndMadExists(string Key,string Key1)
{
bool Flay=false;
SqlConnection conn = new SqlConnection("server=.;database=E_TestData;uid=sa;pwd=system");//启动连接数据库
try
{
conn.Open();//打开连接
SqlCommand cmd = new SqlCommand("usp_SelectTDATA",conn);//启动命令行
cmd.CommandType = CommandType.StoredProcedure;//启动存储过程带参数功能
cmd.Parameters.Add("@SN",Key);//添加T-SQL存储过程参数1
cmd.Parameters.Add("Mac_Address",Key1);//添加T-SQL存储过程参数
cmd.Parameters.Add("@id",222);//添加T-SQL存储过程参数
cmd.Parameters["@id"].Direction = ParameterDirection.Output;//启动输出返回
cmd.ExecuteScalar();
if(((int) cmd.Parameters["@id"].Value)==0)
{
conn.Close();
Flay = true;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
Flay = false;
}
finally
{
conn.Close();
}
return Flay;
}
public void WriteLog(string Key)
{
string path = DateTime.Now.ToString("yyyyMMdd") + @"Data.log";
FileStream fs;
if(File.Exists(path))
{
fs = new FileStream(path,FileMode.Append,FileAccess.Write);//不创建文件写入
}
else
{
fs = new FileStream(path,FileMode.Create,FileAccess.Write);//创建文件写入
}
StreamWriter swMyfile = new StreamWriter(fs);//写入流
string Temp = string.Empty;
Temp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Temp = Temp + Key;
swMyfile.WriteLine(Temp);
swMyfile.Close();
fs.Close();
}
public void FullSqlData(List<string> Key,RichTextBox Key1)
{
string Temp = string.Empty;
this.progressBar2.Value = 0;
this.progressBar2.Maximum = Key.Count;//获取数据长度
int n = 1;
foreach(string str in Key)
{
string[] TestDataField = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
if((IsSnAndMadExists(TestDataField[0],TestDataField[1]))==false)
{
WriteLog(str);
}
else
{
try
{
SqlConnection conn = new SqlConnection("server=.;database=E_TestData;uid=sa;pwd=system");
conn.Open();
SqlCommand cmd = new SqlCommand("usp_InsertTDATA",conn);
cmd.CommandType = CommandType.StoredProcedure;//启动储存过程带参数
cmd.Parameters.Add("@SN",TestDataField[0].Trim());
cmd.Parameters.Add("@Mac_Address",TestDataField[1].Trim());
cmd.Parameters.Add("@State",TestDataField[2].Trim());
cmd.Parameters.Add("@Line",TestDataField[3].Trim());
cmd.Parameters.Add("@Station",TestDataField[4].Trim());
cmd.Parameters.Add("@JobNumber",TestDataField[5].Trim());
cmd.Parameters.Add("@Model",TestDataField[6].Trim());
cmd.Parameters.Add("@Date_Time",TestDataField[7].Trim());
cmd.Parameters.Add("@rs",1);
cmd.Parameters.Add("@rs1",1);
cmd.Parameters["@rs"].Direction = ParameterDirection.Output;
cmd.Parameters["@rs1"].Direction = ParameterDirection.Output;
cmd.ExecuteScalar();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
MessageBox.Show(dr[0].ToString());
dr.Close();
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
output(n, richTextBox1);
this.progressBar2.Value = n;
n++;
}
}
public void output(int Values,RichTextBox Key)
{
if (Values==0)
{
string temp = string.Empty;
temp = DateTime.Now.ToString("HH:mm:ss");
temp += " 进度条开始执行\n";
Key.AppendText(temp);
}
else
{
string temp = string.Empty;
temp = DateTime.Now.ToString("HH:mm:ss");
temp += " 进度进行中[" + MainProgressValues + " / " + Values + "]\n";
Key.AppendText(temp);
}
}
public void WriteReadText(string str)
{
string FileName = DateTime.Now.ToString("yyyyMMdd") + @"FileName.log";
FileStream fs;
if(File.Exists(FileName))
{
fs = new FileStream(FileName,FileMode.Append,FileAccess.Write);//不创建文件
}
else
{
fs = new FileStream(FileName,FileMode.Create,FileAccess.Write);//创建文件
}
StreamWriter sw = new StreamWriter(fs);
string temp = string.Empty;
temp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
temp += str;
sw.WriteLine(temp);
sw.Close();
fs.Close();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
SetProgressInitial(progressBar1, SourcesTextName.Count,0,Color.Green);//主进程设置
progressBar1.Value = 0;
progressBar1.Maximum = SourcesTextName.Count;
output(0,richTextBox1);
foreach(string str in SourcesTextName)
{
WriteReadText(str);//写入读取的文本
FileStream fs = new FileStream(str, FileMode.Open);//打开文本
StreamReader sr = new StreamReader(fs,Encoding.Default);//允许中文方式打开
List<string> Array = new List<string>();
string Temp = string.Empty;
while((Temp=sr.ReadLine())!=null)
{
string[] KeyValues = Temp.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);//截取,或空后面数据
string[] KeyValues8 = KeyValues[7].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);//截取,空后面数据
if (KeyValues8[0].Trim() == KeyValues1 || KeyValues8[0].Trim() == KeyValues2)
Array.Add(Temp);
Temp = string.Empty;
}
FullSqlData(Array, richTextBox1);
Array.RemoveRange(0,Array.Count);//清除所有项
fs.Close();
sr.Close();
progressBar1.Value = MainProgressValues;
MainProgressValues++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
CreateThread1();
}
}
}