1

I would like to create a stored procedure in my database from within C# code.

How can I do this?

FOR INSTANCE: Using Microsoft SQL Server, I just want to create a stored procedure sp_GetUsers in the database that takes an int parameter userid and a simple statement such as

SELECT * FROM Users WHERE ID = @userid

This would be great because I could check the DB for the existence of that stored procedure, and if it doesn't, I just create from within code.

1
  • Create a procedure? Or invoke one? Can you give an example of the scenario? Commented Nov 20, 2012 at 18:52

2 Answers 2

6

You can run arbitrary DDL statements:

new SqlCommand(@"CREATE PROCEDURE ...").ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

Comments

0

This is possible. All you have to do is execute a CREATE PROCEDURE statement via your open DB connection. If you can give us an example of your DB connection/query code right now, along with the DB vendor you're using (i.e. MS SQL, DB2), then we could probably give you a hard and fast example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.