代码之家  ›  专栏  ›  技术社区  ›  Ian Ringrose

如何从ADO.NET调用TSQL函数

  •  17
  • Ian Ringrose  · 技术社区  · 14 年前

    我在SQL Server中定义了一个函数(它接受一个字符串和一个int),如何用ADO.NET调用它?

    (如果它与调用存储过程100%相同,请这样说,因为调用存储过程的例子很多。)

    1 回复  |  直到 14 年前
        1
  •  26
  •   Community CDub    7 年前

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

    见: 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;
      }