黑马程序员技术交流社区
标题:
乱码啊……
[打印本页]
作者:
杨曾荣
时间:
2012-4-3 23:50
标题:
乱码啊……
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条件里读到的数据已成乱码了,什么处理啊
作者:
杨曾荣
时间:
2012-4-5 11:39
解决了,在(StreamReader reader = new StreamReader(fs))再传个指定编码的参数
即(StreamReader reader = new StreamReader(fs,Encoding.Default))
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2