A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨曾荣 中级黑马   /  2012-4-3 23:50  /  1090 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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条件里读到的数据已成乱码了,什么处理啊

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

1 个回复

倒序浏览
解决了,在(StreamReader reader = new StreamReader(fs))再传个指定编码的参数
即(StreamReader reader = new StreamReader(fs,Encoding.Default))
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马