using (FileStream fs = File.OpenRead(ofd.FileName))
{
using (StreamReader reader = new StreamReader(fs))
{
using (SqlConnection conn = new SqlConnection(conString))
{
conn.Open();
using (SqlCommand com = conn.CreateCommand())
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] strs = line.Split('|');
string stuName = strs[0];
int age = Convert.ToInt32(strs[1]);
int scource = Convert.ToInt32(strs[2]);
com.CommandText = "insert into T_Students(stuName,age,scource)values(@stuName,@age,@scource)";
com.Parameters.Clear();
com.Parameters.Add(new SqlParameter("stuName", stuName));
com.Parameters.Add(new SqlParameter("age", age));
com.Parameters.Add(new SqlParameter("scource", scource));
com.ExecuteNonQuery();
}
}
}
}
}
上面的代码添加到数据库的时候,中文出现乱码;把 string[] strs = line.Split('|');
改成 string file = File.ReadAllText(line, Encoding.Default);
string[] strs = file.Split('|');
编译器始终报“含有非法字符”错误;在while条件里读到的数据已成乱码了,什么处理啊 |