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
只有當SessionNumber發生變化時纔會調用Random,這就是我想要的。如果會話編號對於5行是相同的,那麼Random將被第一次調用,而不是再次調用,直到它再次發生變化。 – user167908 2014-08-28 18:53:45
你仍然需要在循環外部創建'Random'。根據需要調用「下一個」。 – 2014-08-28 19:00:24
好的,謝謝!當我回到我的開發機器時,我會嘗試。不過,我不明白我使用Random的方式會如何導致這種事情。它在IF中。如果你喜歡它,解釋器可能派上用場。 – user167908 2014-08-28 20:19:15