我是ASP.NET的新手,在如何在我的ASP.NET Web应用程序中调用内联用户定义函数时遇到了麻烦。
在这里,我在我的函数中传递了两个参数 - 一个是可用的休假(lv),另一个是持续时间(dr)。我只是从lv中减去dr并返回值。但我在调用函数时遇到问题。
我已经尝试过" SELECT dbo.emp_leave_sub(lv,dr)作为剩余的"而不是查询" SELECT dbo.emp_leave_sub(lv,dr)FROM Employee1,其中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();
}
答案 0 :(得分: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();