title: How To create a stored procedure for SQL Server from C# code?
body: <p>I am trying to create a stored procedure in SQL server from a C# winforms application.
  This is the function I have so far.</p>
  
  <pre><code>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();
          }
  }
  </code></pre>
  
  <p>Am I doing this right? I get an error message every time I try to achieve this.</p>
  
  <p>Hey thanks a lot guys!! Its working now..
  This is the code that finally did it..</p>
  
  <pre><code> 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();
          }
      }
  </code></pre>
  
  <p>much appreciated! :)</p>