在 ASP.NET 中调用 UDF

Calling UDF in ASP.NET

我是 ASP.NET 的新手,在如何调用我的 ASP.NET 网络应用程序中的内联用户定义函数方面遇到了问题。

在这里,我在我的函数中传递了两个参数——一个是可用的 leave(lv),另一个是持续时间 (dr)。我只是从 lv 中减去 dr 并返回值。但是我在调​​用函数时遇到问题。

我试过 "SELECT dbo.emp_leave_sub(lv,dr) as remaining" 而不是查询 "SELECT dbo.emp_leave_sub(lv,dr) FROM Employee1 where Employee1.emp_id='" + emp_id + "'" 但它没有用。我不明白我做错了什么。

期待您的回复。任何帮助将不胜感激。

下面是我的函数:

    ALTER FUNCTION dbo.emp_leave_sub(@available int, @duration int)
  RETURNS int
  AS
  -- Returns the availabe leave after deduction for the employee.
  BEGIN
  DECLARE @ret int;
  SELECT @ret = @available - @duration;
  RETURN @ret;
  END;


And this is from where I am calling my function :

    try
            {
                SqlDataReader rdr;
                SqlConnection conn = new SqlConnection (ConfigurationManager.
                ConnectionStrings["PMSConnectionString"].ConnectionString);
                conn.Open();

                string sub_leave = "SELECT dbo.emp_leave_sub(lv,dr) FROM       `  `               Employee1 where Employee1.emp_id='" + emp_id + "'";
                SqlCommand com2 = new SqlCommand(sub_leave, conn);

                com2.CommandType = CommandType.Text;

                using (conn)
                {
                    //read data from the table to our data reader
                    rdr = com2.ExecuteReader();

                    //loop through each row we have read
                    while (rdr.Read())
                    {
                        remaining = rdr.GetInt32(0);
                    }
                rdr.Close();
            }

尝试这样做:

SqlDataReader rdr;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PMSConnectionString"].ConnectionString))
    {
            conn.Open();

            string sub_leave = "SELECT dbo.emp_leave_sub(@available,@duration) FROM Employee1 where Employee1.emp_id=@empid";
            SqlCommand com2 = new SqlCommand(sub_leave, conn);
            com2.Parameters.AddWithValue("@available", your value);
            com2.Parameters.AddWithValue("@duration", your value);
            com2.Parameters.AddWithValue("@empid", emp_id);
            com2.CommandType = CommandType.Text;

               //read data from the table to our data reader
               rdr = com2.ExecuteReader();
             //loop through each row we have read
               while (rdr.Read())
                {
                     remaining = rdr.GetInt32(0);
                }
    }
    rdr.Close();