我们目前有一个非常简单的查询,它返回给定帐户的表的最后一行。
SELECT TOP 1 * FROM tbl WHERE AccountID = @AccountID ORDER BY tblID DESC;
使用参数使用此查询填充 aDataTable
时DataAdapter
,当该帐户的表中没有数据时,它会超时。如果表中有数据,则运行良好。
SqlCommand cmd = new SqlCommand(con);
cmd.commandText = "SELECT TOP 1 * FROM tbl WHERE AccountID = @AccountID ORDER BY tblID DESC"
cmd.Parameters.Add(new SqlParamter("@AccountID", 123));
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt); // TIMES OUT
如果我们更改查询以使其不使用参数,则它可以毫无问题地运行。
"SELECT TOP 1 * FROM tbl WHERE AccountID = 123 ORDER BY tblID DESC"
该表非常大(数百万行),但没有参数的查询几乎立即运行。
任何意见,将不胜感激