sql server - C# - Determining if a Stored Procedure exists -
this question has answer here:
- stored procedure doesn't exist, or it? 3 answers
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
Post a Comment