如何从 ado.net 调用 TSQL 函数

发布于 2024-10-02 01:03:44 字数 113 浏览 0 评论 0原文

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

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2024-10-09 01:03:44

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

请参阅:MySqlCommand 调用函数< /a>

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

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;
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文