我的第一个C#程序:从多个文本文件中读取数据到SqlServer并实现增删改查

              //main.cs
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();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值