0

I am trying to create a stored procedure in SQL server from a C# winforms application. This is the function I have so far.

public void CreateStoredProcedure(string SPname)
{
        try
        {
            string query = "CREATE PROCEDURE " + SPname + " AS SELECT * FROM People WHERE Address='Mumbai'";
            connection.Open();
            var command = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = "EXEC " + query;
            command.ExecuteNonQuery();
        }

        finally
        {
            connection.Close();
        }
}

Am I doing this right? I get an error message every time I try to achieve this.

Hey thanks a lot guys!! Its working now.. This is the code that finally did it..

 public void CreateStoredProcedure(string SPname)
    {
        try
        {
            string query = "CREATE PROCEDURE " + SPname + " AS SELECT * FROM People WHERE Address='Mumbai'";
            connection.Open();
            var command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = query;
            command.ExecuteNonQuery();
            var adapter = new SqlDataAdapter(command);
            adapter.Fill(dt);
            dgv.DataSource = dt;
        }

        finally
        {
            connection.Close();
        }
    }

much appreciated! :)

9
  • 2
    What is the error message? And CommandType.Text is by default. Commented May 23, 2014 at 6:51
  • Oh yes, "SPname" is the name of the stored procedure that I am trying to create. Commented May 23, 2014 at 6:51
  • "ExecuteNonQuery: Connection property has not been initialized." Commented May 23, 2014 at 6:52
  • Use command.Connection = connection; . You didn't connect your SqlCommand with your SqlConnection. Commented May 23, 2014 at 6:52
  • Everytime I try to change the code, I get different error messages. I have a table in SQL server and I want to get the rows where the Address is Mumbai. Commented May 23, 2014 at 6:53

5 Answers 5

1

You do not need EXEC when creating a stored procedure and you need an active connection

Sign up to request clarification or add additional context in comments.

Comments

0

You can try:

 command.CommandType = CommandType.StoredProcedure;

Comments

0

Initialize connection property of your command:

            command.Connection = connection;

Comments

0

Alternatively, you can create a Stored Procedure using Common Language Run-time integration in stead of doing it on the fly.

How to: Create and Run a SQL Server Stored Procedure by using Common Language Run-time Integration

Deploy CLR Stored Procedure to database

In your attempt above the code will only be able to run as a once off as it contains a CREATE command. It must then change to ALTER there after or you need to drop it every time and re-created. this would not be the best practice but just keep in mind.

Comments

0

You need to define a connection object and link it with the command object

    CommandObject.Connection= ConnectionObject;

Also the CommandType.Text is by default.

You could also check if you connection is open using

    if(ConnectionObject.State== ConncetionState.Closed)
       {
        ConnectionObject.Open();
        }

If it is closed, you will need an active Open connection to pass a query.

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.