How do I call a TSQL function from ado.net

前端 未结 1 973
半阙折子戏
半阙折子戏 2020-12-31 03:23

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

相关标签:
1条回答
  • 2020-12-31 03:59

    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;
      } 
    
    0 讨论(0)
提交回复
热议问题