請幫我找到我的邏輯中的缺陷。我有兩個變量名爲「prev」和「next」...我基本上做的是每5秒從數據庫中讀取數據,並使用Websync服務器打印出來,如果next和prev不相等。我的數據庫中有兩行。它看起來像我的程序邏輯有什麼問題
ID
8
10
這裏是鏈接到代碼http://pastebin.com/Hb3eH2Qv
當我運行我的程序,我得到的結果作爲
8 10 8 10
8 10
8 10 8 10
8 10
......(依此類推)
但是,結果應該只是
8 10
我不知道如何8 10 8 10
出現。數據被連接兩次。
注意:您可以只看到PublishLoop()
功能
private void PublishLoop()
{
String prev=String.Copy("");
String next=String.Copy("");
String ConnectionString = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand command = connection.CreateCommand();
command.CommandText = "select ID from Tab1";
command.Notification = null;
while (Running)
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
StreamWriter sw1 = new StreamWriter("C:\\Users\\Thothathri\\Desktop\\next.txt");
while ((reader.Read()))
{
//Response.Write(reader[0].ToString());
next = String.Concat(next,reader[0].ToString());
sw1.WriteLine(next);
}
sw1.Close();
if (!prev.Equals(next))
{
Publisher publisher = new Publisher(new PublisherArgs
{
DomainKey = "c80cb405-eb77-4574-9405-5ba51832f5e6",
DomainName="localhost"
});
Publication publication = publisher.Publish("/test", JSON.Serialize(next));
if (publication.Successful == true)
{
StreamWriter sw = new StreamWriter("C:\\Users\\Thothathri\\Desktop\\error123.txt");
sw.WriteLine("success");
sw.WriteLine(next);
sw.Close();
}
else
{
StreamWriter sw = new StreamWriter("C:\\Users\\Thothathri\\Desktop\\error123.txt");
sw.Write("failed");
sw.Close();
}
prev = String.Copy(next);
next = String.Copy("");
}
}
Thread.Sleep(5000);
}
}
您是否嘗試過使用調試器? – 2011-06-14 07:44:48
是的。邏輯上沒有錯誤。它只是在數據串聯的方式,我猜 – CuriousCoder 2011-06-14 07:45:42
我們不使用pastebin在這裏。直接添加代碼到問題中,只添加相關部分而不是全部代碼。 – jgauffin 2011-06-14 07:46:15