1
所以我有一个函数从数据库中检索数据并输出它。它被封装在try块中,以便处理从行耗尽到处理的错误。尝试块被调用两次
这里有被称为负载的功能(使初始输出),并呼吁后来在更新日志框。
问题是,每当我再次调用函数时,它会输出两次数据。由于只有数据在try块中,而不是头文件,所以指向try/catch是问题。
道歉凌乱/哈克代码:
private void NavigateRecords()
{
da.Fill(ds1, "LogOutput");
textlog4.Text = "Date \t\t Time \t\t Floor\r\n";
try
{
for (int i = 0; i <= 20; i++)
{
DataRow dRow = ds1.Tables["LogOutput"].Rows[i];
for (int i2 = 1; i2 <= 3; i2++)
{
textlog4.Text += dRow.ItemArray.GetValue(i2).ToString() + "\t\t";
}
textlog4.Text += "\r\n";
}
}
catch { textlog4.Text += "----------------------"; }
}
这是做连接的部分的代码,并且可以是使用的:
string sql = "SELECT * From tblLog";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
NavigateRecords();
初始输出:
Date Time Floor
6/12 18:18:22 1
----------------------
输出下次调用时:
Date Time Floor
6/12 18:18:22 1
6/12 18:46:19 2
6/12 18:18:22 1
6/12 18:46:19 2
----------------------
如何调用'NavigateRecords()'以及在哪里使用'ds1'? –
您使用调试器吗?通过你的代码,你会看到发生了什么。 try/catch没有问题。 – roken
如果我猜测,我会说表中有重复的信息,而不是'try'被调用两次。为了记录,我对你依赖程序逻辑的异常并不狂热。 –