有没有办法用更少的行编写以下代码?执行这样一个简单的查询似乎需要很多代码。没有 LINQ,因为我使用的是 VS2005。VB 或 C# 中的答案都是可以接受的。
Using cmd As DbCommand = oDB.CreateCommand()
cmd.CommandText = "SELECT * FROM [Table1] WHERE [Date] BETWEEN @Date1 AND @Date2"
cmd.CommandTimeout = 30
cmd.CommandType = CommandType.Text
cmd.Connection = oDB
Dim param As DbParameter
param = cmd.CreateParameter()
param.Direction = ParameterDirection.Input
param.DbType = DbType.Date
param.ParameterName = "@Date1"
param.Value = Now().Date
cmd.Parameters.Add(param)
param = cmd.CreateParameter()
param.Direction = ParameterDirection.Input
param.DbType = DbType.Date
param.ParameterName = "@Date2"
param.Value = Now().Date.AddDays(intDaysAhead)
cmd.Parameters.Add(param)
End Using
Dim reader As DbDataReader = cmd.ExecuteReader()