【发布时间】:2011-06-14 07:42:38
【问题描述】:
请帮我找出我的逻辑缺陷。我有两个名为“prev”和“next”的变量......我基本上做的是每 5 秒从我的数据库中读取一次数据,如果 next 和 prev 不相等,则使用 Websync 服务器将其打印出来。我的数据库中有两行。好像
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);
}
}
【问题讨论】:
-
你试过使用调试器吗?
-
是的。逻辑上没有错误。我猜这只是数据连接的方式
-
我们这里不使用 pastebin。将代码直接添加到问题中,并且只添加相关部分而不是所有代码。
-
我认为你应该在
while ((reader.Read()))之前清除next -
@Reniuz:我正在这样做。在 Thread.sleep(5000) 之上,我给出 prev=String.Copy(next);和 next=String.Copy("");