How do I check that a stored procedure exists using ADO.Net? Is there a specific way to do this (apart from e开发者_JAVA百科xecuting SELECT OBJECT_ID() and checking the result)?
You can use the INFORMATION_SCHEMA
views like so:
Select *
From INFORMATION_SCHEMA.ROUTINES
Where ROUTINE_NAME = '<your procedure name>'
If you're asking whether or not there's a way to check without using any SQL at all, then the answer is no.
You could use SQL Management Objects if you really want a code-only solution, but that's a pretty heavy dependency to add simply to check for the existence of a stored procedure - and it's just going to issue SQL behind the scenes anyway.
It's best if you simply use a SqlCommand
.
Or if you don't care about adhering to SQL standard constructs (INFORMATION_SCHEMA), you can use the built-in SQL Server system catalog view (2005 and up) to check:
SELECT object_id, name
FROM sys.procedures
WHERE name = 'your-procedure-name-here'
精彩评论