Exemplo n.º 1
0
        public void TestBuildToFile()
        {
            this.tracer.WriteLine("测试方法 BuildToFile(string, Regex, string, string)");

            try
            {
                File.WriteAllText(@"temp.txt", "小马:22\n小红:25", Encoding.Default);
                CodingHelper.BuildToFile("INSERT INTO student (name, age) VALUES ('$1', $2)", new Regex("(\\w+):(\\w+)$", RegexOptions.Multiline), @"temp.txt", @"output.txt");

                this.tracer.WriteLine(string.Format("成功: 转化文件 temp.txt 为 sql 脚本并输出到 output.txt, 内容为 {0}, 由于没有指定编码, 所以可能出现乱码, 无法正常转化", File.ReadAllText(@"output.txt", Encoding.Default)));
            }
            catch (Exception err)
            { this.tracer.WriteLine(string.Format("异常: {0}", err.Message)); }


            this.tracer.WriteLine("测试方法 BuildToFile(string, Regex, string, string, Encoding)");

            try
            {
                File.WriteAllText(@"temp.txt", "小马:22\n小红:25", Encoding.Default);
                CodingHelper.BuildToFile("INSERT INTO student (name, age) VALUES ('$1', $2)", new Regex("(\\w+):(\\w+)$", RegexOptions.Multiline), @"temp.txt", @"output.txt", Encoding.UTF8);

                this.tracer.WriteLine(string.Format("成功: 转化文件 temp.txt 为 sql 脚本并输出到编码为 UTF8 的 output.txt, 内容为 {0}", File.ReadAllText(@"output.txt", Encoding.UTF8)));
            }
            catch (Exception err)
            { this.tracer.WriteLine(string.Format("异常: {0}", err.Message)); }


            this.tracer.WriteLine("测试方法 BuildToFile(string, Regex, string, Encoding, string)");

            try
            {
                File.WriteAllText(@"temp.txt", "小马:22\n小红:25", Encoding.UTF8);
                CodingHelper.BuildToFile("INSERT INTO student (name, age) VALUES ('$1', $2)", new Regex("(\\w+):(\\w+)$", RegexOptions.Multiline), @"temp.txt", Encoding.UTF8, @"output.txt");

                this.tracer.WriteLine(string.Format("成功: 转化文件 UTF8 的 temp.txt 为 sql 脚本并输出到编码为 output.txt, 内容为 {0}", File.ReadAllText(@"output.txt", Encoding.Default)));
            }
            catch (Exception err)
            { this.tracer.WriteLine(string.Format("异常: {0}", err.Message)); }


            this.tracer.WriteLine("测试方法 BuildToFile(string, Regex, string, Encoding, string, Encoding)");

            try
            {
                File.WriteAllText(@"temp.txt", "小马:22\n小红:25", Encoding.UTF8);
                CodingHelper.BuildToFile("INSERT INTO student (name, age) VALUES ('$1', $2)", new Regex("(\\w+):(\\w+)$", RegexOptions.Multiline), @"temp.txt", Encoding.UTF8, @"output.txt", Encoding.UTF8);

                this.tracer.WriteLine(string.Format("成功: 转化文件 UTF8 的 temp.txt 为 sql 脚本并输出到编码为 UTF8 的 output.txt, 内容为 {0}", File.ReadAllText(@"output.txt", Encoding.UTF8)));
            }
            catch (Exception err)
            { this.tracer.WriteLine(string.Format("异常: {0}", err.Message)); }


            this.tracer.WriteLine("测试方法 BuildToFile(string, string, Regex, string)");

            try
            {
                CodingHelper.BuildToFile("INSERT INTO student (name, age) VALUES ('$1', $2)", "小马:22\n小红:25", new Regex("(\\w+):(\\w+)$", RegexOptions.Multiline), @"output.txt");

                this.tracer.WriteLine(string.Format("成功: 转化文本 '小马:22\\n小红:25' 为 sql 脚本并输出到 output.txt, 内容为 {0}, 由于没有指定编码, 所以可能出现乱码, 无法正常转化", File.ReadAllText(@"output.txt", Encoding.Default)));
            }
            catch (Exception err)
            { this.tracer.WriteLine(string.Format("异常: {0}", err.Message)); }


            this.tracer.WriteLine("测试方法 BuildToFile(string, string, Regex, string, Encoding)");

            try
            {
                CodingHelper.BuildToFile("INSERT INTO student (name, age) VALUES ('$1', $2)", "小马:22\n小红:25", new Regex("(\\w+):(\\w+)$", RegexOptions.Multiline), @"output.txt", Encoding.Unicode);

                this.tracer.WriteLine(string.Format("成功: 转化文本 '小马:22\\n小红:25' 为 sql 脚本并输出到 output.txt, 内容为 {0}", File.ReadAllText(@"output.txt", Encoding.Unicode)));
            }
            catch (Exception err)
            { this.tracer.WriteLine(string.Format("异常: {0}", err.Message)); }

            this.tracer.WaitPressEnter();
        }