【问题标题】:Extracting substrings from a block of text in C#从 C# 中的文本块中提取子字符串
【发布时间】:2013-08-29 18:55:59
【问题描述】:
INSERT INTO Pax.SalesDocumentCoupon VALUES ('67c4158a-5d9f-4302-aaba-8fcddc0af552','3','TZX','SAW','','','EAOW','','00137','07-May-2013 12:00:00 AM','','','','E','','','','','','','','624','07-May-2013 12:00:00 AM','00137','TZX','SAW','Y','','F','N','False','X','fc12d5b5-c677-4207-81f4-5207a4f0705d','2481624807','6242481624807')
Filename : C:\Users\Relate 18\Desktop\PC DATA FILES\SAC\SAC0805201301_00002.txt
Table Name : Pax.SalesDocumentCoupon
Sql Stmt  :  INSERT INTO Pax.SalesDocumentCoupon VALUES ('67c4158a-5d9f-4302-aaba-8fcddc0af552','3','TZX','SAW','','','EAOW','','00137','07-May-2013 12:00:00 AM','','','','E','','','','','','','','624','07-May-2013 12:00:00 AM','00137','TZX','SAW','Y','','F','N','False','X','fc12d5b5-c677-4207-81f4-5207a4f0705d','2481624807','6242481624807')
Error Msg : Violation of PRIMARY KEY constraint 'PK_SalesDocumentCoupon'. Cannot insert duplicate key in object 'Pax.SalesDocumentCoupon'. The duplicate key value is (67c4158a-5d9f-4302-aaba-8fcddc0af552, 3).
The statement has been terminated.

 INSERT INTO Pax.SalesDocumentCoupon VALUES ('67c4158a-5d9f-4302-aaba-8fcddc0af552','4','SAW','BRU','','','GNBAGT','','00801','07-May-2013 12:00:00 AM','','','','G','','','','','','','','624','07-May-2013 12:00:00 AM','00801','SAW','BRU','Y','','F','N','False','X','f05365cd-9570-4b1c-b109-6ace5eaf1ea5','2481624807','6242481624807')
Filename : C:\Users\Relate 18\Desktop\PC DATA FILES\SAC\SAC0805201301_00002.txt
Table Name : Pax.SalesDocumentCoupon
Sql Stmt  :  INSERT INTO Pax.SalesDocumentCoupon VALUES ('67c4158a-5d9f-4302-aaba-8fcddc0af552','4','SAW','BRU','','','GNBAGT','','00801','07-May-2013 12:00:00 AM','','','','G','','','','','','','','624','07-May-2013 12:00:00 AM','00801','SAW','BRU','Y','','F','N','False','X','f05365cd-9570-4b1c-b109-6ace5eaf1ea5','2481624807','6242481624807')
Error Msg : Violation of PRIMARY KEY constraint 'PK_SalesDocumentCoupon'. Cannot insert duplicate key in object 'Pax.SalesDocumentCoupon'. The duplicate key value is (67c4158a-5d9f-4302-aaba-8fcddc0af552, 4).
The statement has been terminated.

我有上面的文件(还有更多的块,我只显示了 2 个,因为其他块也一样) 我需要提取 SQL 语句以及附加的错误消息。

到目前为止,我尝试了以下代码

        while ((sql_line = SR.ReadLine()) != null)
        {
            string RequiredLine1 = "Sql Stmt";
            //sql_line = SR.ReadLine();
            if (sql_line.Contains(RequiredLine1))
            {
                //crop out string that comes before:
                int index = sql_line.IndexOf(":") + 1;
                sql_line = sql_line.Substring(index);
            }
        }


        while ((error_line = SR.ReadLine()) != null)
        {

            string RequiredLine2 = "Error Msg";
            // error_line = SR.ReadLine();
            if (error_line.Contains(RequiredLine2))
            {
                //crop out string that comes before:
                int index = error_line.IndexOf(":") + 1;
                error_line = error_line.Substring(index);
            }
        }

但它不起作用。 请帮忙。

【问题讨论】:

  • 什么不起作用?你所做的只是重新分配一个变量然后什么都不做

标签: c# asp.net c#-4.0 text-files substring


【解决方案1】:

您需要在一个while 语句中执行此操作,否则您将永远不会得到错误行,并且您需要将 sql 语句放在不同的参数中:

    string RequiredLine2 = "Error Msg";
    string RequiredLine1 = "Sql Stmt";

    while ((sql_line = SR.ReadLine()) != null)
    {
        if (sql_line.Contains(RequiredLine1))
        {
            //crop out string that comes before:
            int index = sql_line.IndexOf(":") + 1;
            sql_stmt_line = sql_line.Substring(index);
        }

        // error_line = SR.ReadLine();
        if (error_line.Contains(RequiredLine2))
        {
            //crop out string that comes before:
            int index = error_line.IndexOf(":") + 1;
            error_line = error_line.Substring(index);
        }
    }

我还将RequiredLine2/1 放在while 语句之外,因为一遍又一遍地创建相同的字符串是没有意义的。

另外,如果你想从文件中提取多个 sql 语句,你需要一些数据结构来保存它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多