2014-08-28 2 views
0

아래의 방법에서 데이터를 Excel 파일의 목록으로 가져옵니다. txnhdr.SessionNo가 변경 될 때마다 SessionNo를 변경하고 싶습니다. 이것은 IF 문에서 처리됩니다.oledbdatareader 위의 루프가 잠자기를 필요로하므로 자체적으로 앞서 가지 않습니다.

이 메서드는 때때로 작동하지만 다른 경우에는 IF 내부가 손상되지 않고 Session이 변경되지 않습니다. Thread.Sleep을 넣지 않으면 매번 작동합니다.

내가 누락 된 것을 볼 수 없습니다.

public static void LoadDataFromFile(string testXLDataFile, string sheetName) 
    { 
     string path = Directory.GetCurrentDirectory() + @"\TestData\" + testXLDataFile; 

     var connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';"; 
     var queryString = "select * from [" + sheetName + "]"; 

     using (OleDbConnection connection = new OleDbConnection(connStr)) 
     { 
      OleDbCommand command = new OleDbCommand(queryString, connection); 

      connection.Open(); 
      OleDbDataReader txnReader = command.ExecuteReader(); 

      int? SessionNo = -1; 
      int currSessionNo = -2; 

      while (txnReader.Read()) 
      {      

       var txnhdr = new TxnHeader(txnReader["HostIP"].ToString().Trim(), int.Parse(txnReader["SessionNo"].ToString().Trim()), txnReader["CustID"].ToString().Trim(), txnReader["GroupID"].ToString().Trim(), txnReader["ProfID"].ToString().Trim()); 

       if (currSessionNo != txnhdr.SessionNo) 
       { 
        currSessionNo = txnhdr.SessionNo; 
        SessionNo = new Random().Next(999999, 999999999); 
       } 

       //the rest of the code does not reassign any of the above variables 
       //but if I don't put a Thread.Sleep(100) here, then SessionNo will 
       //not be reassigned a new random number starting at random rows. 

       }//while 
      }//using 
      } //method 

답변

0

문제는 데이터베이스 코드와 관련이 없습니다. 매번 루프를 통해 Random의 새 인스턴스를 만듭니다.

루프 앞에 Random이라는 인스턴스를 하나 만들고 루프에서 Next()을 호출하십시오.

+0

랜덤은 SessionNumber가 변경 될 때만 호출됩니다. 그게 내가 원하는 것입니다. 세션 번호가 5 행에 대해 동일하면 Random이 처음으로 호출되고 다시 변경 될 때까지 다시 호출되지 않습니다. – user167908

+1

여전히 루프 외부에 '랜덤'을 생성해야합니다. 필요하면 '다음'이라고 불러주세요. –

+0

그래, 고마워! 내가 내 dev 컴퓨터로 돌아 가면 그것을 시도 할 것이다. 아직도, 난 무작위를 사용하는 방법이 이런 종류의 일이 발생할 것이라고 이해하지 않습니다. 그것은 IF 내부에 있습니다. 당신이 그것 같이 느끼는 경우에, 설명자는 경편하게 들어올 것입니다. – user167908