博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(15 Mins)A Simple C# And SQL Test
阅读量:5863 次
发布时间:2019-06-19

本文共 9158 字,大约阅读时间需要 30 分钟。

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();        }        ///         /// Replace string1 with string2 in all the files in a given folder including all the subfolders        ///         /// Root folder        /// The string before replacing        /// The string after replaced        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);            }        }        ///         /// Check whether the file is txt file using fileName        ///         /// File Name        /// 
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();        }        ///         /// Replace string1 with string2 in all the files in a given folder including all the subfolders        ///         /// Root folder        /// The string before replacing        /// The string after replaced        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:

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 databaseCREATE DATABASE smu_db_testGOUSE smu_db_testGO--Create all the tablesCREATE TABLE student  (     studentid   INT NOT NULL,     studentname NVARCHAR (10),     gender      CHAR(1),     mobileno    NVARCHAR(13)  )GOALTER TABLE student  ADD CONSTRAINT pk_studentid PRIMARY KEY (studentid)GOCREATE TABLE programme  (     programmeid   INT NOT NULL,     programmename NVARCHAR(30)  )GOALTER TABLE programme  ADD CONSTRAINT pk_programmeid PRIMARY KEY (programmeid)GOCREATE TABLE student_pro  (     studentid   INT NOT NULL,     programmeid INT NOT NULL  )GOALTER TABLE student_pro  ADD CONSTRAINT fk_studentid_student_pro FOREIGN KEY (studentid) REFERENCES  student (studentid)GOALTER TABLE student_pro  ADD CONSTRAINT fk_programmeid_student_pro FOREIGN KEY (programmeid) REFERENCES  programme (programmeid)GO  --Insert data into student tableINSERT INTO student (studentid, studentname, gender,mobileno) VALUES (1,  'Jason', 'M','12345678')GOINSERT INTO student (studentid, studentname, gender,mobileno) VALUES (2,  'Kitty', 'F','')GOINSERT INTO student (studentid, studentname, gender,mobileno) VALUES (3,  'Nance', 'M','')GOINSERT INTO student (studentid, studentname, gender,mobileno) VALUES (4,  'Harry', 'F','22334466')GO--Insert data into programme tableINSERT INTO programme (programmeid, programmename) VALUES (1,  'English')GOINSERT INTO programme (programmeid, programmename) VALUES (2,  'Chinese')GO--Insert data into student_pro tableINSERT INTO student_pro (studentid, programmeid) VALUES (1,  1)GOINSERT INTO student_pro (studentid, programmeid) VALUES (2,  2)GOINSERT INTO student_pro (studentid, programmeid) VALUES (3,  1)GOINSERT INTO student_pro (studentid, programmeid) VALUES (4,  2)GO

 

The database and all the tables after processed the above SQL

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 #ofStudentsmobileFROM programme AORDER BY A.programmeid

 

The Results as below:

你可能感兴趣的文章
GRUB Legacy
查看>>
关于 error: LINK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决方案...
查看>>
我的友情链接
查看>>
hexo博客解决不蒜子统计无法显示问题
查看>>
python实现链表
查看>>
java查找string1和string2是不是含有相同的字母种类和数量(string1是否是string2的重新组合)...
查看>>
Android TabActivity使用方法
查看>>
java ShutdownHook介绍与使用
查看>>
Eclipse的 window-->preferences里面没有Android选项
查看>>
《麦田里的守望者》--[美]杰罗姆·大卫·塞林格
查看>>
[置顶] 深入探析Java线程锁机制
查看>>
遇到的那些坑
查看>>
央行下属的上海资信网络金融征信系统(NFCS)签约机构数量突破800家
查看>>
[转] Lazy evaluation
查看>>
常用查找算法总结
查看>>
grep 零宽断言
查看>>
被神话的大数据——从大数据(big data)到深度数据(deep data)思维转变
查看>>
修改校准申请遇到的问题
查看>>
【DL-CV】浅谈GoogLeNet(咕咕net)
查看>>
python大佬养成计划----win下对数据库的操作
查看>>