Is there a way to write the following code in less lines? It seems like a lot of code to execute such a simple query. No LINQ as I am using VS2005. Answers in either VB or C# are acceptable.
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()
These are probably the fewest lines you can get:
(assuming
SqlClient
but similar for other data providers)