To pass a parameter to a stored procedure in C#, you can use the SqlCommand class to execute the stored procedure and add parameters to the command object. Here's an example:
using System.Data.SqlClient; // Create a new SqlCommand object with the stored procedure name and connection string SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection); cmd.CommandType = CommandType.StoredProcedure; // Add parameters to the command object cmd.Parameters.AddWithValue("@Parameter1", value1); cmd.Parameters.AddWithValue("@Parameter2", value2); // Add additional parameters as needed // Execute the stored procedure and process the results using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // Process the results } } In this example, we create a new SqlCommand object called cmd with the name of the stored procedure and a SqlConnection object called connection. We set the CommandType property of the cmd object to CommandType.StoredProcedure to indicate that we are executing a stored procedure.
We then add parameters to the cmd object using the Parameters.AddWithValue method. We specify the name of the parameter (including the "@" symbol) and the value of the parameter. Note that you can add as many parameters as needed to the cmd object.
Finally, we execute the stored procedure using the cmd.ExecuteReader method, which returns a SqlDataReader object that we can use to process the results of the stored procedure. Inside the while loop, we can access the values returned by the stored procedure using the SqlDataReader methods like GetString, GetInt32, etc.
"C# SqlCommand parameterized stored procedure example"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@ParameterName", parameterValue); connection.Open(); command.ExecuteNonQuery(); } } "C# SqlDataAdapter with stored procedure parameters"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlDataAdapter adapter = new SqlDataAdapter("YourStoredProcedure", connection)) { adapter.SelectCommand.CommandType = CommandType.StoredProcedure; adapter.SelectCommand.Parameters.AddWithValue("@ParameterName", parameterValue); DataSet dataSet = new DataSet(); adapter.Fill(dataSet); } } "C# stored procedure input parameters with SqlDataReader"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@ParameterName", parameterValue); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // Process results } } } } "C# stored procedure output parameters with SqlCommand"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@InputParameter", inputParameterValue); SqlParameter outputParameter = new SqlParameter("@OutputParameter", SqlDbType.Int); outputParameter.Direction = ParameterDirection.Output; command.Parameters.Add(outputParameter); connection.Open(); command.ExecuteNonQuery(); int result = Convert.ToInt32(outputParameter.Value); } } "C# SqlClient stored procedure with input and output parameters"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@InputParameter", inputParameterValue); SqlParameter outputParameter = new SqlParameter("@OutputParameter", SqlDbType.Int); outputParameter.Direction = ParameterDirection.Output; command.Parameters.Add(outputParameter); connection.Open(); command.ExecuteNonQuery(); int result = Convert.ToInt32(outputParameter.Value); } } "C# stored procedure with table-valued parameters"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; DataTable dataTable = new DataTable(); // Populate dataTable with data SqlParameter tableParameter = new SqlParameter("@TableParameter", SqlDbType.Structured); tableParameter.Value = dataTable; command.Parameters.Add(tableParameter); connection.Open(); command.ExecuteNonQuery(); } } "C# stored procedure with XML parameter"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@XmlParameter", xmlStringValue); connection.Open(); command.ExecuteNonQuery(); } } "C# stored procedure with multiple parameters"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@Param1", value1); command.Parameters.AddWithValue("@Param2", value2); connection.Open(); command.ExecuteNonQuery(); } } "C# stored procedure with datetime parameter"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@DateTimeParameter", datetimeValue); connection.Open(); command.ExecuteNonQuery(); } } "C# stored procedure with dynamic SQL and parameters"
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourDynamicSQLStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@Param1", value1); command.Parameters.AddWithValue("@Param2", value2); connection.Open(); command.ExecuteNonQuery(); } } break joi modalpopup kendo-ui soapui powerbi azure-pipelines-yaml algorithms xlpagertabstrip inno-setup