Call a SQL scalar value function from C#
Example of how to call a SQL scalar value function using the SqlCommand object in C#.int parameter1 = 5; // holds the parameter value that is passed to the SQL function
// open the SQL connection & command objects.
using (SqlConnection conn = CustomFunctionToGetConnection())
using (SqlCommand cmd = conn.CreateCommand())
{
// set the connection properties
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandTimeout = 60000;
// set the command text
cmd.CommandText = "select ShipDate = dbo.GetNextShipDate(@param1)";
// set the parameters
cmd.Parameters.AddWithValue("@param1", parameter1);
// run the sql
object o = cmd.ExecuteScalar();
// get the results.
DateTime results = (DateTime)o;
}
Last modified by Mohit @ 4/5/2025 12:09:09 PM