------- WindowsPhone 7手机开发、.Net培训、期待与您交流! -------
1.数据导入(filestream自己研究下)
<1>选择文件对象
<2>File.Readlines()读取文件(文件编码方式要考虑)ANSI UTF-8返回类型 IEnumerable<>的接口
[查:File.Readlines() IEnumerable<> ]
<3>读取文本获取所需的字段
<4>插入DB操作
[附:tab键转义字符\t,文件里是俺制表符践行分割,而非空格]
[http://msdn.microsoft.com/zh-cn/library/dd383503.aspx
备注
ReadLines 和 ReadAllLines 方法不同点在于:当使用 ReadLines 时,您可以在返回整个集合之前开始枚举字符串集合;当您使用 ReadAllLines 时,则必须等待整个字符串数组都被返回后才能访问数组。因此,在处理非常大的文件时,ReadLines 可能更高效。
您可以使用 ReadLines 方法执行以下操作:
对文件执行 LINQ to Objects 查询以获取筛选过的行集。
使用 File.WriteAllLines(String, IEnumerable<String>) 方法将返回的行集合写入文件,使用 File.AppendAllLines(String, IEnumerable<String>) 方法将它们追加到现有文件。
创建集合立即被填充的实例,该集合采用其构造函数的 IEnumerable<T> 集合,例如 IList<T> 或 Queue<T>。
]
c# Code
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.txt";
if (ofd.ShowDialog()==false)//出错在哪里???为什么???
{
return;
}
else
{
string[] lines = File.ReadAllLines(ofd.FileName.ToString(), Encoding.Default).ToArray();
for (int i = 1; i < lines.Count(); i++)
{
string line = lines[i];
string[] strs = line.Split('\t');
string startTelNum = strs[0].ToString();
string city = strs[1].ToString();
city = city.Trim('"');
string telTytpe = strs[2].ToString();
telTytpe = telTytpe.Trim('"');
string QuHao = strs[3].ToString();
QuHao = QuHao.Trim('"');
省市联动.SqlHelper.ExecuNonQuery(@"insert into dbo.tb_Tel
values(@HaoDuan,@Address,@Type,@QuHao)",
new SqlParameter("@HaoDuan", startTelNum),
new SqlParameter("@Address", city),
new SqlParameter("@Type", telTytpe),
new SqlParameter("@QuHao", QuHao));
}
MessageBox.Show("ok");
}
}