locked
Parameterized Queries Against Redbrick Database RRS feed

  • Question

  • User-1407477457 posted

    Has anyone ever accomplised this?  Sample code is at the bottom and I'm either doing something wrong or attempting something unachievable.  Hopefully it's the former.  I'm very new at dot net.

    I am using redbrick odbc driver version 5.63.0010.  We also have this driver installed on a ColdFusion server where we can run parameterized queries.   

    The code below runs successfully.  However, if I uncomment the line where I attempt to use the @Code parameter, I get this exception:

    ** ERROR ** (87) Invalid syntax '...SERVICE WHERE SERVICE_CODE=<==>@'.

    This is the same error I get if I run that query against the database outside of dot net.  So, what's wrong with this code?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.Odbc;


    namespace DanBracukTestConsole
    {
        class Program
        {
            static void Main(string[] args)
            {

                string DWConnectionString =
                    "DSN=RBdw;Uid=something;Pwd=something_else";


                string DWQuery = "select count(*) TheCount from service where service_code  = 20";
         // DWQuery = "select count(*) TheCount from service where service_code  = @Code";           
               
                OdbcConnection DWConnection = new OdbcConnection();
                DWConnection.ConnectionString = DWConnectionString;
                OdbcDataAdapter DWDataAdaptor = new OdbcDataAdapter();
                OdbcDataReader DWDataReader = null;
                OdbcCommand DWCommand = new OdbcCommand(DWQuery, DWConnection);
               
                OdbcParameter MyParam = new OdbcParameter("@Code", OdbcType.SmallInt);
                DWCommand.Parameters.Add(MyParam).Value=20;

                DWConnection.Open();
                try
                {
                    DWDataReader = DWCommand.ExecuteReader();

                    while (DWDataReader.Read())
                    {
                        Console.WriteLine("hello " + DWDataReader["TheCount"].ToString());
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    DWConnection.Close();
                }

                Console.Read();
            }
        }
    }

    Tuesday, August 30, 2011 9:32 AM

Answers