有时候我们会把文件中的数据导入到数据库中,那么这要如何做呢?下面进行详细分析。
private void button1_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "文本文件|*.txt"; if (ofd.ShowDialog() != true) { return; } string filename = ofd.FileName; IEnumerable<string> lines = File.ReadLines(filename,Encoding.Default); int i = 0; foreach (string line in lines) { string[] segs = line.Split(‘|‘); string name = segs[0]; int age = Convert.ToInt32(segs[1]); SqlHelper.ExecuteNonQuery("Insert into T_Customer (name,age) values(@name,@age)", new SqlParameter("@name",name), new SqlParameter("@age",age)); i++; } MessageBox.Show("导入成功!共导入"+i+"条数据"); }
原文:http://www.cnblogs.com/kaolalovemiaomiao/p/4707580.html