sql server - C# - Determining if a Stored Procedure exists -


this question has answer here:

i've written dbcontext extension try , determine if stored procedure exists within associated database.

    public static bool storedprocedureexists(this dbcontext input, string name)     {         int exists = input.database.executesqlcommand(string.format("select top 1 * [sys].[objects] [type_desc] = 'sql_stored_procedure' , [name] = '{0}';", name));          //return true; // if exists, else false     } 

the problem is: regardless of whether stored procedure name exists or not, exists variable (returned execsqlcommand) contains '-1'. unable determine if stored procedure in database or not.

executing generated query in sql server management studio works expected, returning 1 row if stored procedure exists , no rows if if not.

does have ideas on how achieve (programmatically determine if stored procedure exists database) ?

thanks rob

thanks help... in end got work follows:

    public static bool storedprocedureexists(this dbcontext input, string name)     {         var query = input.database.sqlquery(             typeof(int),              string.format("select count(*) [sys].[objects] [type_desc] = 'sql_stored_procedure' , [name] = '{0}';", name),              new object[] {});          int exists = query.cast<int>()             .single();          return (exists > 0);     } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -