using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace FileStream
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
string content = null;
string connstring = "server=***;database=***;uid=sa;pwd=***";
private void Read_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string File_Detail = folderBrowserDialog1.SelectedPath;
DirectoryInfo dinfo = new DirectoryInfo(File_Detail);
FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
// openFileDialog1.Filter = "文本文件(*.dat)|*.dat";
foreach (FileSystemInfo fsinfo in fsinfos )
// if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//使用“打开”对话框中选择的文件实例化StreamReader对象
StreamReader sr = new StreamReader(fsinfo.FullName, Encoding.Default);
string delcontent=null;
content = sr.ReadLine();
while (content != null)
{
// content = sr.ReadLine();
string[] split = content.Split(new Char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);//split user
int m = split.Length - 1;
if (m < 0)
{
return;
}
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
string[] arry = split[m].Split('=');// split time and user
string[] arry1 = split[0].Split('=');
for (int i = 0; i < arry.Length; i = i + 2)
{
string strsql = @"
if not exists(select 1 from Login_records where IP=@ip and MAC=@mac and OA_User=@oa_user and Login_Time=@login_time)
insert into Login_records(Login_Time,OA_User,IP,MAC) VALUES (@login_time,@oa_user,@ip,@mac)"; //添加内容
// string Strsql = @"insert into Login_records (Login_Time=@login_time, OA_User=@oa_user,IP=@ip,MAC=@mac)";'" + arry[i] + "','" + arry[i + 1] + "','" + arry1[0] + "','" + arry1[1] + "'
SqlCommand cmd = new SqlCommand(strsql, conn);
cmd.Parameters.Add("@login_time", arry[i]);
cmd.Parameters.Add("@oa_user", arry[i + 1]);
cmd.Parameters.Add("@ip", arry1[0]);
cmd.Parameters.Add("@mac", arry1[1]);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
if (split.Length == 3)
{
string strsql_PC = "update Login_records set PC_Name=@PC_Name where Login_Time=@login_time";//判断有无计算机名,有则添加至数据库
SqlCommand cmd_PC = new SqlCommand(strsql_PC, conn);
cmd_PC.Parameters.Add("@PC_Name", split[1]);
cmd_PC.Parameters.Add("@login_time", arry[i]);
cmd_PC.ExecuteNonQuery();
cmd_PC.Parameters.Clear();
}
} ///for循环结束
delcontent = content;
content = sr.ReadLine();
conn.Close();
} //while循环结束
sr.Close(); //关闭当前文件读取流
string scontent = delcontent;
StreamWriter sw = new StreamWriter(fsinfo.FullName, false, Encoding.Default);
sw.WriteLine(delcontent);
sw.Close();
} //文件遍历结束
MessageBox.Show("读取文件成功!");
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("请在IP框中输入IP。");
}
private void bunIP_Click(object sender, EventArgs e) //按IP查询
{
DataSet dsList = Help_Class.ExecuteDataSet("select * from Login_records where IP='"+ text_IP.Text+"' order by Login_Time DESC", null);
dgvList.DataSource = dsList.Tables[0];
text_IP.Text = "";
}
private void btnUser_Click(object sender, EventArgs e) //按用户名查询
{
Frm_Uchek from2 = new Frm_Uchek();
from2.Show();
}
private void btn_Del_Click(object sender, EventArgs e) //删除一月前的数据
{
string time = DateTime.Now.AddMonths(-1).ToString("yyyyMMddhhmm"); //加n个月
int dsobj = Help_Class.ExecuteNonQuery("delete from Login_records where Login_Time<'"+ time +"'", null);
MessageBox.Show("删除数据成功!");
}
private void bun_Time_Click(object sender, EventArgs e) //按时间查询
{
Frm_Tchek from3 = new Frm_Tchek();
from3.Show();
}
}
}