0

我有richtextbox1 并想删除所有的“USER1”。从下面看,如果句子以 ALTER TABLE 开头并包含“USER1”。使用 C# 的最佳方法是什么

我在richtextbox 上的文字

DROP INDEX XIE2TBL_A;

DROP INDEX XPKTBL_A;

ALTER TABLE "COURSE" DROP CONSTRAINT "CRSE_CRSE_FK";

ALTER TABLE "ENROLLMENT" DROP CONSTRAINT "ENR_STU_FK";

ALTER TABLE "ENROLLMENT" DROP CONSTRAINT "ENR_SECT_FK";

ALTER TABLE "ENROLLMENT" ADD CONSTRAINT "ENR_STU_FK" FOREIGN KEY ("STUDENT_ID") REFERENCES "USER1"."STUDENT"("STUDENT_ID") ENABLE;

ALTER TABLE "ENROLLMENT" ADD CONSTRAINT "ENR_SECT_FK" FOREIGN KEY ("SECTION_ID") REFERENCES "USER1"."SECTION"("SECTION_ID") ENABLE;

ALTER TABLE "GRADE" DROP CONSTRAINT "GR_ENR_FK";

ALTER TABLE "GRADE" DROP CONSTRAINT "GR_GRTW_FK";

ALTER TABLE "GRADE" ADD CONSTRAINT "GR_ENR_FK" FOREIGN KEY ("STUDENT_ID","SECTION_ID") REFERENCES "USER1"."ENROLLMENT"("STUDENT_ID","SECTION_ID") ENABLE;

ALTER TABLE "GRADE" ADD CONSTRAINT "GR_GRTW_FK" FOREIGN KEY ("SECTION_ID","GRADE_TYPE_CODE") REFERENCES "USER1"."GRADE_TYPE_WEIGHT"("SECTION_ID","GRADE_TYPE_CODE") ENABLE;

ALTER TABLE "GRADE_TYPE_WEIGHT" DROP CONSTRAINT "GRTW_GRTYP_FK";

ALTER TABLE "GRADE_TYPE_WEIGHT" DROP CONSTRAINT "GRTW_SECT_FK";

ALTER TABLE "GRADE_TYPE_WEIGHT" ADD CONSTRAINT "GRTW_GRTYP_FK" FOREIGN KEY ("GRADE_TYPE_CODE") REFERENCES "USER1"."GRADE_TYPE"("GRADE_TYPE_CODE") ENABLE;

ALTER TABLE "GRADE_TYPE_WEIGHT" ADD CONSTRAINT "GRTW_SECT_FK" FOREIGN KEY ("SECTION_ID") REFERENCES "USER1"."SECTION"("SECTION_ID") ENABLE;

ALTER TABLE "INSTRUCTOR" DROP CONSTRAINT "INST_ZIP_FK";

ALTER TABLE "INSTRUCTOR" ADD CONSTRAINT "INST_ZIP_FK" FOREIGN KEY ("ZIP") REFERENCES "USER1"."ZIPCODE"("ZIP") ENABLE;

ALTER TABLE "SECTION" DROP CONSTRAINT "SECT_INST_FK";

我要这个:

  DROP INDEX XIE2TBL_A;

DROP INDEX XPKTBL_A;

ALTER TABLE "COURSE" DROP CONSTRAINT "CRSE_CRSE_FK";

ALTER TABLE "ENROLLMENT" DROP CONSTRAINT "ENR_STU_FK";

ALTER TABLE "ENROLLMENT" DROP CONSTRAINT "ENR_SECT_FK";

ALTER TABLE "ENROLLMENT" ADD CONSTRAINT "ENR_STU_FK" FOREIGN KEY ("STUDENT_ID") REFERENCES "STUDENT"("STUDENT_ID") ENABLE;

ALTER TABLE "ENROLLMENT" ADD CONSTRAINT "ENR_SECT_FK" FOREIGN KEY ("SECTION_ID") REFERENCES "SECTION"("SECTION_ID") ENABLE;

ALTER TABLE "GRADE" DROP CONSTRAINT "GR_ENR_FK";

ALTER TABLE "GRADE" DROP CONSTRAINT "GR_GRTW_FK";

ALTER TABLE "GRADE" ADD CONSTRAINT "GR_ENR_FK" FOREIGN KEY ("STUDENT_ID","SECTION_ID") REFERENCES "ENROLLMENT"("STUDENT_ID","SECTION_ID") ENABLE;

ALTER TABLE "GRADE" ADD CONSTRAINT "GR_GRTW_FK" FOREIGN KEY ("SECTION_ID","GRADE_TYPE_CODE") REFERENCES "GRADE_TYPE_WEIGHT"("SECTION_ID","GRADE_TYPE_CODE") ENABLE;

ALTER TABLE "GRADE_TYPE_WEIGHT" DROP CONSTRAINT "GRTW_GRTYP_FK";

ALTER TABLE "GRADE_TYPE_WEIGHT" DROP CONSTRAINT "GRTW_SECT_FK";

ALTER TABLE "GRADE_TYPE_WEIGHT" ADD CONSTRAINT "GRTW_GRTYP_FK" FOREIGN KEY ("GRADE_TYPE_CODE") REFERENCES "GRADE_TYPE"("GRADE_TYPE_CODE") ENABLE;

ALTER TABLE "GRADE_TYPE_WEIGHT" ADD CONSTRAINT "GRTW_SECT_FK" FOREIGN KEY ("SECTION_ID") REFERENCES "SECTION"("SECTION_ID") ENABLE;

ALTER TABLE "INSTRUCTOR" DROP CONSTRAINT "INST_ZIP_FK";

ALTER TABLE "INSTRUCTOR" ADD CONSTRAINT "INST_ZIP_FK" FOREIGN KEY ("ZIP") REFERENCES "ZIPCODE"("ZIP") ENABLE;

ALTER TABLE "SECTION" DROP CONSTRAINT "SECT_INST_FK";
4

2 回答 2

0

对于您richTextBox1.Text,我们不需要检查每行是否以开头,ALTER TABLE只需执行以下操作:

 richTextBox1.Text = richTextBox1.Text.Replace("REFERENCES \"USER1\".", "REFERENCES ");

如果你想检查ALTER TABLE,使用一点RegEx

Regex reg = new Regex("(ALTER TABLE .+ REFERENCES\\s+)\"USER1\"[.](.+)");
richTextBox1.Text = reg.Replace(richTextBox1.Text, "$1$2");            
于 2013-08-20T02:41:51.830 回答
0

你可以尝试使用这个

        public string Remove(string statement, string toRemove)
    {
        int start = statement.IndexOf(toRemove);
        int end = toRemove.Length;
        statement = statement.Remove(start, end);
        return statement;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (string line in richTextBox1.Text.Split('\n'))
        {
            if (line.Split(' ')[0] == "ALTER" && line.Split(' ')[1] == "TABLE" && line.Contains("USER1"))
            {
                int start = richTextBox1.Text.IndexOf(line);
                int end = line.Length - 1;
                richTextBox1.Text = richTextBox1.Text.Remove(start, end).Insert(start, Remove(line, "\"USER1\"."));
            }
        }
    }

这应该可以,我已经测试过了。

于 2013-08-20T02:57:29.187 回答