0

我是 ASP.NET 的新手,在如何在我的 ASP.NET Web 应用程序中调用内联用户定义函数时遇到了麻烦。

在这里,我在函数中传递了两个参数——一个是可用的 leave(lv),另一个是 duration (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();
            }
4

1 回答 1

1

尝试这样做:

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();   
于 2015-09-08T00:38:05.793 回答