日期:2014-05-18 浏览次数:20867 次
openFileDialog1.Filter = "文本文件(*.txt)|*.txt"; if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; } using(FileStream fileStream = File.OpenRead(openFileDialog1.FileName)) { using(StreamReader streamReader = new StreamReader(fileStream, Encoding.Default)) { string ConnStr = ConfigurationManager.ConnectionStrings["IP连接字符串"].ConnectionString; using(SqlConnection conn = new SqlConnection(ConnStr)) { using(SqlCommand cmd = new SqlCommand("Insert into IPAddress(StartIP,EndIP,Country,Name) values (@StartIP,@EndIP,@Country,@Name)",conn)) { string line = null; conn.Open(); while ((line = streamReader.ReadLine()) != null) { string[] strs = line.Split(' '); string Start = strs[0]; string End = strs[1]; string Country = strs[2]; string Name = strs[3]; cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("StartIP",Start)); cmd.Parameters.Add(new SqlParameter("EndIP", End)); cmd.Parameters.Add(new SqlParameter("Country", Country)); cmd.Parameters.Add(new SqlParameter("Name", Name)); cmd.ExecuteNonQuery(); } } MessageBox.Show("导入成功!"); } } } }