Nick Vaccaro
Nick Vaccaro

Reputation: 5504

ODBC Command Does Not Accept Parameter

I'm attempting to call a stored proc on my local SQL Server (2008 R2) instance from C# (.NET 3.5) via ODBC connection. The problem I'm encountering is that the stored proc doesn't seem to be receiving the input parameter.

I have the profiler set up as well - not seeing any input params making it to the database.

What's happening! : (

PS - Please don't suggest using any different technologies.


App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MYDB" connectionString="Driver={SQL Server};Server=localhost;Database=MYDB;Uid=user_name;Pwd=password;"/>
  </connectionStrings>
</configuration>

Program.cs

using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;

namespace DatabaseTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string mssqlConnectionString = ConfigurationManager.ConnectionStrings["MYDB"].ConnectionString;

            using (OdbcConnection connection = new OdbcConnection(mssqlConnectionString))
            {
                connection.Open();

                using (OdbcCommand command = new OdbcCommand("usp_Get_UserInfo", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandTimeout = 0;
                    command.Parameters.Add(new OdbcParameter("@username", OdbcType.VarChar, 32) { Value = "Bob", IsNullable = true,  });

                    using (OdbcDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string userName = reader["USER_NAME"].ToString();
                            string userInfo = reader["USER_INFO"].ToString();

                            Console.WriteLine(String.Format("{0} | {1}", 
                                userName, userInfo));
                        }

                        reader.Close();
                    }
                }

                connection.Close();
            }
        }
    }
}

Stored Proc

USE [MYDB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_Get_UserInfo] 
    @username varchar(32) = null 
AS
BEGIN

    SET NOCOUNT ON;

    SELECT u.[USER_NAME]
        , u.USER_INFO
    FROM dbo.UserDataTable u
        WHERE u.[USER_NAME] = ISNULL(@username, u.[USER_NAME)

END

Results

[USER_NAME] | [USER_INFO]
Alice | Alice's info
Bob | Bob's info
Charlie | Charlie's info

Upvotes: 1

Views: 2980

Answers (1)

Nick Vaccaro
Nick Vaccaro

Reputation: 5504

Found the answer after some intense googling.

Execute Parameterized SQL StoredProcedure via ODBC

It seems that the odbc connection requires stored procedures to be called in a very interesting manner.

I wasn't getting the same error as the one in that question because my stored proc had a single nullable parameter. Hence, I wasn't getting any error at all.

using (OdbcCommand command = new OdbcCommand("{call usp_Get_UserInfo (?)}", connection))

Upvotes: 1

Related Questions