请帮我找到我的逻辑中的缺陷。我有两个变量名为“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