string FILE_NAME = textBox1.Text.Trim(); if (!File.Exists(FILE_NAME)) //如果不文件存在,则抛出异常 { MessageBox.Show("文件不存在!"); } StreamReader sr = File.OpenText(FILE_NAME); String input; int i = 0; int j = 0; string myConnectionString = "Pooling='false';Data Source=127.0.0.1,1433;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa"; SqlConnection myConnection = new SqlConnection(myConnectionString); if (myConnection.State == ConnectionState.Closed) myConnection.Open(); while ((input = sr.ReadLine()) != null) { //Console.WriteLine(input); MatchCollection des_imgs = null; string regstr = ""; Regex reg = new Regex(""); Match match = null; //IP regstr = "//d{0,3}//.//d{0,3}//.//d{0,3}//.//d{0,3}"; reg = new Regex(@regstr, RegexOptions.IgnoreCase); match = reg.Match(input); string aip = match.Groups[0].Value; //Page regstr = "GET /(.*?) - 80"; reg = new Regex(@regstr, RegexOptions.IgnoreCase); match = reg.Match(input); string page = match.Groups[1].Value; //time regstr = "(.*?) W3SVC"; reg = new Regex(@regstr, RegexOptions.IgnoreCase); match = reg.Match(input); string time = match.Groups[1].Value; if (aip.Length > 0) { string Query = "insert into tbip(ip,page,iptime) values('" + aip + "','" + page + "','" + time + "')"; SqlCommand cmd = new SqlCommand(Query, myConnection); cmd.ExecuteNonQuery(); } //MessageBox.Show(time); j++; } MessageBox.Show("完成!共读取" + j + "行。"); sr.Close(); if (myConnection.State == ConnectionState.Open) myConnection.Close(); select count(*),ip from tbip group by ip order by count(*) desc