如何从ado.net调用TSQL函数

Ian*_*ose 17 sql-server ado.net

我有一个在SQL Server中定义的函数(需要一个字符串和一个int)如何用ADO.NET调用它?

(如果它与调用存储过程100%相同,请说明,因为有很多关于调用存储过程的例子)

Chr*_*ter 26

唯一的区别是您必须为返回值添加特殊参数

请参阅:MySqlCommand调用函数

  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;
  } 
Run Code Online (Sandbox Code Playgroud)