6
Answers

problem with paramters on select statement

tforrester

tforrester

19y
2.7k
1
I have just started learning C#, coming from Java. I am trying to do a select statement in the way of a prepared statement which is the kind of thing done in Java all the time. I am not sure if what I am doing is legal, or if it is just the usage I have got wrong: The Error is: Error getting Ticknum: ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ':'. The code snipits are: dbConn = new OdbcConnection(); dbConn.ConnectionString = connectionString; dbConn.Open(); string query = "select convert(varchar,TICKNUM) TICKNUM from TICKET where convert(datetime,DATEVAL) >= :lowDate and convert(datetime,DATEVAL) <= :hiDate"; IDbCommand command = dbConn.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = query; IDbDataParameter lowDate = command.CreateParameter(); lowDate.ParameterName = ":lowDate"; lowDate.DbType = DbType.DateTime; lowDate.Value = param1; command.Parameters.Add(lowDate); IDbDataParameter hiDate = command.CreateParameter(); hiDate.ParameterName = ":hiDate"; hiDate.DbType = DbType.DateTime; hiDate.Value = param2; command.Parameters.Add(hiDate); command.Prepare(); IDataReader reader = command.ExecuteReader(); Many thanks in advance for your help.
Answers (6)