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.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; }