2013-08-27 38 views
3
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語句信息。從文本中的C#塊

到現在我嘗試了下面的代碼

 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); 
      } 
     } 

但它無法正常工作。 請幫忙。

+0

*什麼*不工作?你所做的就是重新分配一個變量,然後什麼都不做 – Sayse

回答

5

你需要做的是在一個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); 
     } 
    } 

我也把while語句之外的RequiredLine2/1,因爲一遍又一遍地創建相同的字符串沒有意義。

另外,如果你想從文件中提取一個以上的SQL語句,你會需要一些數據結構來保持在