Home Forums Programming General how to run a stored procedure from C# application RE: how to run a stored procedure from C# application

  • If all you want to do is execute a stored proc that returns no results then you don't need a DataAdapter/DataSet (you don't necessarily need one even if you do want a resultset -- it depends on what you want to do with the results). You also don't need the "Exec" in your command text is you set the CommandType property of your SqlCommand to "StoredProcedure".

    [font="Courier New"]string connectionString = "(your connection string here)";

    string commandText = "usp_YourStoredProc";

    using (SqlConnection conn = new SqlConnection(connectionString))

    {

    SqlCommand cmd = new SqlCommand(commandText, conn);

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.CommandTimeout = 600;

    conn.Open();

    int affectedRows = cmd.ExecuteNonQuery();

    conn.Close();

    }[/font]

    If you want to pass parameters, it'd also be better to define SqlParameter objects for the parameters, set their values, then add those to the command object, rather than concatenating them into the command text.