我有一个 asp.net 页面,它加载一个我们知道从 SQL Server Management Studio 执行时可以正常运行的查询(在 1 到 2 秒内),但是当在 SQLCommand 中从 ASP.NET 执行时,查询需要更长的时间,我不能自问题开始以来,似乎弄清楚除了一行已添加到查询中之外发生了什么,但是我无法找到问题所在。
添加的有问题的代码行是第 6 行:bi.INGR_CODE != 0
SQL 语句
SELECT bh.JOB_NUMBER, j.DESCRIPTION, SUM(bi.INGR_ACTUAL) AS TOTAL
FROM BATCH_HEADER AS bh LEFT OUTER JOIN
BATCH_INGR AS bi
ON bh.BATCH_ID = bi.BATCH_ID AND
bh.FACTORY = bi.FACTORY AND
bi.INGR_CODE <> 0 LEFT OUTER JOIN
ServerNameReplaced.man_prod.dbo.JOBS AS j
ON bh.JOB_NUMBER = j.JOB_NUMBER COLLATE database_default AND
bh.FACTORY = j.FACTORY COLLATE database_default
WHERE ( bh.FACTORY = @Factory ) AND
( bh.DATETIME_DUMP >= @StartDate ) AND
( bh.DATETIME_DUMP < @EndDate )
GROUP BY bh.JOB_NUMBER, j.DESCRIPTION
ORDER BY bh.JOB_NUMBER
ASP.NET 代码隐藏文件
//Temporary List
List<BatchItem> data = new List<BatchItem>();
string SQLCommand = DBHelper.LoadSQLStatement( "batchdescription.sql" );
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringNameReplaced"].ConnectionString);
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(SQLCommand, sqlConnection );
try
{
sqlCommand.Parameters.Add( "@StartDate", System.Data.SqlDbType.DateTime ).Value = StartDate;
sqlCommand.Parameters.Add( "@EndDate ", System.Data.SqlDbType.DateTime ).Value = EndDate;
sqlCommand.Parameters.Add( "@Factory", System.Data.SqlDbType.VarChar, 2 ).Value = Factory;
sqlConnection.Open();
SqlDataReader DataReader = sqlCommand.ExecuteReader();
while ( DataReader.Read() )
{
data.Add(
new BatchItem()
{
JobNumber = DataReader[0].ToString(),
Description = DataReader[1].ToString(),
Total = decimal.Parse( DataReader[2].ToString() )
} );
}
}
catch ( Exception ex )
{
//handle exceptions
}
finally
{
sqlConnection.Close();
}