我有一个这样的 SQL 查询:
set @fromdate = '2012/01/01 00:00:00'
set @todate = '2013/01/01 00:00:00'
SELECT TableOne.date_time, TableTwo.*
FROM TableOne
INNER JOIN TableTwo ON TableOne.gprs_id = TableTwo.recordid
WHERE date_time >= @fromdate AND date_time <= @todate
我在 .net 4 (c#) 中使用它,如下所示:
string strSQL = "SELECT TableOne.date_time, TableTwo.* FROM TableOne INNER JOIN TableTwo ON " +
"TableOne.gprs_id = TableTwo.recordid where date_time >= @fromdate AND date_time <= @todate";
var pCommand = pConnectionWrapper.DBConnection.CreateCommand();
pCommand.CommandText = strSQL;
var paramFromDate = pCommand.CreateParameter();
paramFromDate.ParameterName = "@fromdate";
paramFromDate.DbType = DbType.DateTime;
paramFromDate.SourceColumn = "date_time";
paramFromDate.Value = fromDate;
pCommand.Parameters.Add(paramFromDate);
var paramToDate = pCommand.CreateParameter();
paramToDate.ParameterName = "@todate";
paramToDate.DbType = DbType.DateTime;
paramToDate.SourceColumn = "date_time";
paramToDate.Value = toDate;
pCommand.Parameters.Add(paramToDate);
var pReader = pCommand.ExecuteReader();
while (pReader.Read())
{
var someValue = double.Parse(pReader["somevalue"].ToString());
var date = DateTime.Parse(pReader["date_time"].ToString());
var someObject = new someObject(someValue, someDate);
someObjects.Add(comeObject);
}
在 SQL Server Management Studio 中运行此查询比在 .net 中运行快得多。从 .net 遍历行非常慢。我认为这个内部连接会导致这个(?),因为我的其他不包含连接的查询在.net 下非常快。
我可以提高 .net 的查询性能吗?像这样使用数据集而不是阅读器或 sg?
非常感谢!