Ejemplo n.º 1
0
        /// <summary>
        /// 从文件夹直接写到.sql文件
        /// </summary>
        /// <param name="infilepath"></param>
        /// <param name="encoding"></param>
        /// <param name="moveold"></param>
        /// <returns></returns>
        public long FromFileToSQLFile(string infilepath, long splitlinesnum, Encoding encoding, bool moveold = true)
        {
            string[] alllines = new string[splitlinesnum];
            List<string> tempresult = new List<string>();
            long result = 0;

            using (StreamReader srReadFile = new StreamReader(infilepath, encoding))
            {
                string tmp;
                int i = 0;
                do
                {
                    //每次读splitlinesnum行写入sql文件
                    while (((tmp = srReadFile.ReadLine()) != null) && tmp.Length > 0 && i < splitlinesnum)
                    {
                        alllines[i] = tmp;
                        i++;
                    }
                    result += i;

                    Parallel.ForEach<string>(alllines, line =>
                    {
                        if (line != null)
                        {
                            //针对不同数据源修改
                            //过滤斜杠 剔除坏数据
                            string[] cols = LineSplit(line);
                            bool isbaddata = IsBadData(cols);
                            if (!isbaddata)
                            {
                                for (int k = 0; k < 2; k++)
                                {
                                    cols[k] = ReplaceSlash(cols[k]);
                                }

                                StringBuilder sb = new StringBuilder();
                                sb.AppendFormat("insert into users(username,nickname,truename,`password`,salt,isencrypt,question1,question2,answer1,answer2,phone,email,qq,idcard,`from`,ip) values ('{0}','{1}','{2}','{3}','{4}',{5},'{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}');"
                                                       , cols[0]        //username
                                                       , string.Empty       //nickname
                                                       , string.Empty   //truename
                                                       , cols[1]        //password
                                                       , string.Empty   //salt
                                                       , "0"            //isencrypt
                                                       , string.Empty   //question1
                                                       , string.Empty   //question2
                                                       , string.Empty   //answer1
                                                       , string.Empty   //answer2
                                                       , string.Empty   //phone
                                                       , cols[0].Contains("@") ? cols[0] : string.Empty    //email [email protected]
                                                       , cols[0].Contains("@qq.com") ? cols[0].Substring(0, cols[0].IndexOf("@")) : string.Empty //qq
                                                       , string.Empty//idcard
                                                       , "网易"//from
                                                       , string.Empty);//ip
                                string newline = sb.ToString();

                                lock (tempresult)
                                {
                                    tempresult.Add(newline);
                                }
                            }
                        }
                    });

                    FileHandler file = new FileHandler();
                    //单个输出文件不大于6G,总文件不大于60G
                    for (int j = 0; j < 10; j++)
                    {
                        string outfilepath = Path.GetDirectoryName(infilepath) + "\\" + "sql_" + (j + 1).ToString() + ".sql";
                        if (File.Exists(outfilepath))
                        {
                            FileInfo fi = new FileInfo(outfilepath);
                            //5G
                            if (fi.Length >= file.FILE_LIMIT_TO_WRITE)
                            {
                                continue;
                            }
                        }
                        file.WriteToFile(tempresult, outfilepath, encoding);
                        break;
                    }

                    tempresult.Clear();
                    i = 0;
                } while (tmp != null);
            }

            if (moveold)
            {
                FileHandler file = new FileHandler();
                file.MoveFile(infilepath, Path.GetDirectoryName(infilepath) + "\\old\\" + Path.GetFileName(infilepath));
            }
            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从单个文件导出成sql(OLD)
        /// </summary>
        /// <param name="infilepath"></param>
        /// <param name="encoding"></param>
        /// <param name="moveold"></param>
        /// <returns></returns>
        public List<string> FromSingleFileToSQLText(string infilepath, Encoding encoding, bool moveold = true)
        {
            List<string> lines = new List<string>();
            List<string> result = new List<string>();

            using (StreamReader srReadFile = new StreamReader(infilepath, encoding))
            {
                string tmp;
                while ((tmp = srReadFile.ReadLine()) != null)
                {
                    lines.Add(tmp);
                }
            }

            Parallel.ForEach<string>(lines, line =>
            {
                //针对不同数据源修改
                string[] cols = LineSplit(line);

                bool isbaddata = isbaddata = IsBadData(cols);
                if (!isbaddata)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        cols[i] = ReplaceSlash(cols[i]);
                    }
                    //string newline = string.Format("insert into users(username,nickname,truename,password,salt,isencrypt,question1,question2,answer1,answer2,phone,email,qq,idcard,from,ip) values ('{0}','{1}','{2}','{3}','{4}',{5},'{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}');\r\n"
                    //                       , cols[0]        //username
                    //                       , string.Empty       //nickname
                    //                       , string.Empty   //truename
                    //                       , cols[1]        //password
                    //                       , string.Empty   //salt
                    //                       , "0"            //isencrypt
                    //                       , string.Empty   //question1
                    //                       , string.Empty   //question2
                    //                       , string.Empty   //answer1
                    //                       , string.Empty   //answer2
                    //                       , string.Empty   //phone
                    //                       , cols[0].Contains("@") ? cols[0] : string.Empty    //email [email protected]
                    //                       , cols[0].Contains("@qq.com") ? cols[0].Substring(0, cols[0].IndexOf("@")) : string.Empty //qq
                    //                       , string.Empty//idcard
                    //                       , "网易"//from
                    //                       , string.Empty);//ip
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("insert into users(username,nickname,truename,`password`,salt,isencrypt,question1,question2,answer1,answer2,phone,email,qq,idcard,`from`,ip) values ('{0}','{1}','{2}','{3}','{4}',{5},'{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}');"
                                           , cols[0]        //username
                                           , string.Empty       //nickname
                                           , string.Empty   //truename
                                           , cols[1]        //password
                                           , string.Empty   //salt
                                           , "0"            //isencrypt
                                           , string.Empty   //question1
                                           , string.Empty   //question2
                                           , string.Empty   //answer1
                                           , string.Empty   //answer2
                                           , string.Empty   //phone
                                           , cols[0].Contains("@") ? cols[0] : string.Empty    //email [email protected]
                                           , cols[0].Contains("@qq.com") ? cols[0].Substring(0, cols[0].IndexOf("@")) : string.Empty //qq
                                           , string.Empty//idcard
                                           , "网易"//from
                                           , string.Empty);//ip
                    string newline = sb.ToString();

                    lock (result)
                    {
                        result.Add(newline);
                    }
                    newline = null;
                }
            });
            if (moveold)
            {
                FileHandler file = new FileHandler();
                file.MoveFile(infilepath, Path.GetDirectoryName(infilepath) + "\\old\\" + Path.GetFileName(infilepath));
            }
            return result;
        }