• (15 Mins)A Simple C# And SQL Test


    C# Basic Test

    Replace string1 with string2 in all the files in a given folder including all the subfolders

    The first way:

    /********************************************************************************
     * Copyright (C) xxx. All rights reserved.
     * 
     * Author: xxx 
     * Email : xxx
     * Blog  : http://www.cnblogs.com/KnightsWarrior/
     * Create Date: 28/08/2012 18:40:24 PM
     * Description: Replace string1 with string2 in all the files in a given folder
     *              including all the subfolders
     *          
     * Revision History:
     *      Date                       Author               Description
     *     28/08/2012 19:01:24 PM      KnightsWarrior       
    *********************************************************************************/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ReplaceStingInTxtFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                //string rootFolder = @"C:\TempFolder";
                //string string1 = "he is";
                //string string2 = "she is";
                Console.WriteLine("Please input the root folder:");
                string rootFolder = Console.ReadLine();
                Console.WriteLine("Please input the string you want to replace:");
                string string1 = Console.ReadLine();
                Console.WriteLine("Please input the string you want to replace with:");
                string string2 = Console.ReadLine();
                Console.WriteLine("Replacing started...");
                ReplaceString(rootFolder, string1, string2);
                Console.WriteLine("Replacing finished...");
                Console.ReadLine();
            }
    
            /// <summary>
            /// Replace string1 with string2 in all the files in a given folder including all the subfolders
            /// </summary>
            /// <param name="rootFolder">Root folder</param>
            /// <param name="string1">The string before replacing</param>
            /// <param name="string2">The string after replaced</param>
            private static void ReplaceString(string rootFolder, string string1, string string2)
            {
                DirectoryInfo d = new DirectoryInfo(rootFolder);
                FileInfo[] fis = d.GetFiles();
                foreach (var fi in fis)
                {
                    if (IsTxtFile(fi.Name))
                    {
                        try
                        {
                            string contents = File.ReadAllText(fi.FullName);
                            contents = contents.Replace(string1, string2);
                            File.SetAttributes(fi.FullName, FileAttributes.Normal);
                            File.WriteAllText(fi.FullName, contents);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
    
                DirectoryInfo[] ds = d.GetDirectories();
                foreach (var info in ds)
                {
                    ReplaceString(info.FullName, string1, string2);
                }
            }
    
            /// <summary>
            /// Check whether the file is txt file using fileName
            /// </summary>
            /// <param name="fileName">File Name</param>
            /// <returns></returns>
            private static bool IsTxtFile(string fileName)
            {
                if (fileName.IndexOf(".txt")>0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
        }
    }

    The second way:

    /********************************************************************************
     * Copyright (C) XXX. All rights reserved.
     * 
     * Author: XXX 
     * Email : XXX
     * Blog  : http://www.cnblogs.com/KnightsWarrior/
     * Create Date: 28/08/2012 19:01:24 PM
     * Description: Replace string1 with string2 in all the files in a given folder
     *              including all the subfolders
     *          
     * Revision History:
     *      Date                       Author               Description
     *     28/08/2012 19:09:24 PM      KnightsWarrior       
    *********************************************************************************/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ReplaceStingInTxtFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                //string rootfolder = @"C:\TempFolder";
                //string string1 = "You are";
                //string string2 = "He is";
                Console.WriteLine("Please input the root folder:");
                string rootFolder = Console.ReadLine();
                Console.WriteLine("Please input the string you want to replace:");
                string string1 = Console.ReadLine();
                Console.WriteLine("Please input the string you want to replace with:");
                string string2 = Console.ReadLine();
                Console.WriteLine("Replacing started...");
                ReplaceString(rootFolder, string1, string2);
                Console.WriteLine("Replacing finished...");
                Console.ReadLine();
            }
    
            /// <summary>
            /// Replace string1 with string2 in all the files in a given folder including all the subfolders
            /// </summary>
            /// <param name="rootFolder">Root folder</param>
            /// <param name="string1">The string before replacing</param>
            /// <param name="string2">The string after replaced</param>
            public static void ReplaceString(string rootfolderName, string string1, string string2)
            {
                //Get all the txt files
                string[] files = Directory.GetFiles(rootfolderName, "*.txt", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    try
                    {
                        //Read the file into string
                        string contents = File.ReadAllText(file);
                        contents = contents.Replace(string1, string2);
    
                        //Set attributes and write the string into the file
                        File.SetAttributes(file, FileAttributes.Normal);
                        File.WriteAllText(file, contents);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
    }

    The test folder as below:

    image

    DB Basic Test

    /********************************************************************************
     * Copyright (C) XXX. All rights reserved.
     * 
     * Author: XXX 
     * Email : XXX
     * Blog  : http://www.cnblogs.com/KnightsWarrior/
     * Create Date: 28/08/2012 18:40:24 PM
     * Description: Create database and all the tables and insert simple data to these tables
     *          
     * Revision History:
     *      Date                       Author               Description
     *     28/08/2012 19:01:24 PM      KnightsWarrior       
    *********************************************************************************/
    --Create a database
    CREATE DATABASE smu_db_test
    
    GO
    
    USE smu_db_test
    
    GO
    
    --Create all the tables
    CREATE TABLE student
      (
         studentid   INT NOT NULL,
         studentname NVARCHAR (10),
         gender      CHAR(1),
         mobileno    NVARCHAR(13)
      )
    
    GO
    
    ALTER TABLE student
      ADD CONSTRAINT pk_studentid PRIMARY KEY (studentid)
    
    GO
    
    CREATE TABLE programme
      (
         programmeid   INT NOT NULL,
         programmename NVARCHAR(30)
      )
    
    GO
    
    ALTER TABLE programme
      ADD CONSTRAINT pk_programmeid PRIMARY KEY (programmeid)
    
    GO
    
    CREATE TABLE student_pro
      (
         studentid   INT NOT NULL,
         programmeid INT NOT NULL
      )
    
    GO
    
    ALTER TABLE student_pro
      ADD CONSTRAINT fk_studentid_student_pro FOREIGN KEY (studentid) REFERENCES
      student (studentid)
    
    GO
    
    ALTER TABLE student_pro
      ADD CONSTRAINT fk_programmeid_student_pro FOREIGN KEY (programmeid) REFERENCES
      programme (programmeid)
    
    GO  
    
    --Insert data into student table
    INSERT INTO student (studentid, studentname, gender,mobileno) VALUES (1,  'Jason', 'M','12345678')
    GO
    INSERT INTO student (studentid, studentname, gender,mobileno) VALUES (2,  'Kitty', 'F','')
    GO
    INSERT INTO student (studentid, studentname, gender,mobileno) VALUES (3,  'Nance', 'M','')
    GO
    INSERT INTO student (studentid, studentname, gender,mobileno) VALUES (4,  'Harry', 'F','22334466')
    GO
    
    --Insert data into programme table
    INSERT INTO programme (programmeid, programmename) VALUES (1,  'English')
    GO
    INSERT INTO programme (programmeid, programmename) VALUES (2,  'Chinese')
    GO
    
    --Insert data into student_pro table
    INSERT INTO student_pro (studentid, programmeid) VALUES (1,  1)
    GO
    INSERT INTO student_pro (studentid, programmeid) VALUES (2,  2)
    GO
    INSERT INTO student_pro (studentid, programmeid) VALUES (3,  1)
    GO
    INSERT INTO student_pro (studentid, programmeid) VALUES (4,  2)
    GO

    The database and all the tables after processed the above SQL

    image

    The Query SQL:

    SELECT A.programmeid,
           A.programmename,
          (SELECT COUNT(B.studentid)
           FROM student B
           INNER JOIN student_pro C ON B.studentid = C.studentid
           AND C.programmeid =A.programmeid) AS #ofStudents,
          (SELECT COUNT(B.studentid)
           FROM student B
           INNER JOIN student_pro C ON B.studentid = C.studentid
           AND C.programmeid =A.programmeid
           AND B.gender = 'M') AS #ofStudentsmale,
          (SELECT COUNT(B.studentid)
           FROM student B
           INNER JOIN student_pro C ON B.studentid = C.studentid
           AND C.programmeid =A.programmeid
           AND B.gender = 'F') AS #ofStudentsfemale,
          (SELECT COUNT(B.studentid)
           FROM student B
           INNER JOIN student_pro C ON B.studentid = C.studentid
           AND C.programmeid =A.programmeid
           AND B.mobileno <> '' AND B.mobileno IS NOT NULL) AS #ofStudentsmobile
    FROM programme A
    ORDER BY A.programmeid

    The Results as below:

    image


    作者:圣殿骑士
    出处:http://www.cnblogs.com/KnightsWarrior/
    关于作者:专注于微软平台项目架构、管理和企业解决方案。自认在面向对象, 面向服务以及微服务领域有一定的造诣,熟悉设计模式、TDD、极限编程、领域驱动、架构设计、敏捷开发和项目管理。现主要从事.NET/.NET Core, Go, JavaScript/TypeScript, Azure/AWS等云计算方面的项目开发、架构、管理和企业培训工作。如有问题或建议,请多多赐教!
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:KnightsWarrior(at)msn(dot)com  微博:圣殿骑士微博  联系我,非常感谢。

  • 相关阅读:
    C#委托本质探索 四、方法变量内、外混合调用
    calibredrv 对layer做操作
    2021年11月工作笔记
    2022年1月工作资料
    2021年12月工作资料
    MySQL 5.7 MGR原理及部署
    在虚拟机上安装redis集群,redis使用版本为4.0.5,本机通过命令客户端可以连接访问,外部主机一直访问不了
    mongoDB中的的常用语法
    使用Nginx做图片服务器时候,配置之后图片访问一直是 404问题解决
    jquery.cookie() 方法的使用(读取、写入、删除)
  • 原文地址:https://www.cnblogs.com/KnightsWarrior/p/ASimpleTest.html
Copyright © 2020-2023  润新知