Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a TSQL function from ado.net

I have a function defined in SQL Server (that takes a string and a int) how do I call it with ADO.NET?

(If it is 100% same as calling a stored proc, please just say so, as there a lot of examples on calling stored procs about)

like image 725
Ian Ringrose Avatar asked Nov 10 '10 14:11

Ian Ringrose


1 Answers

The only difference is that you must have a special paramter added for the return value

See: MySqlCommand call function

  using (var connection = new SqlConnection("ConnectionString"))
  using (var command = connection.CreateCommand())
  {
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "MyFunction";

    SqlParameter returnValue = command.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
    returnValue.Direction = ParameterDirection.ReturnValue;

    connection.Open();
    command.ExecuteNonQuery();

    return returnValue.Value;
  } 
like image 50
Chris Baxter Avatar answered Sep 19 '22 12:09

Chris Baxter