OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.txt";
if (ofd.ShowDialog() != true)
{
return;
}
string filename = ofd.FileName;
//File.ReadAllLines()是把文件一次读取到string集合中
IEnumerable<string> lines = File.ReadLines(filename,Encoding.Default);
foreach (string line in lines)
{//把"小红|16"按照|分割
string[] str = line.Split('|');
string name = str[0];
string age = str[1];
SqlHelper.ExecuteNonQuery("insert into Tusername(Name, Age) values(@Name,@Age)",
new SqlParameter("@Name", name),
new SqlParameter("@Age", Convert.ToInt32(age)));
}
MessageBox.Show("导入成功!导入" + lines.Count() + "条数据");
出错如下:
WindowsFormsApplication1.DAL.SqlHelper”的类型初始值设定项引发异常。
|