0

私は ASP.NET の初心者で、ASP.NET Web アプリケーションでインライン ユーザー定義関数を呼び出す方法に問題があります。

ここでは、関数に 2 つの引数を渡しました。1 つは利用可能な leave(lv) で、もう 1 つは期間 (dr) です。lv から dr を差し引いて値を返すだけです。しかし、関数の呼び出しに問題があります。

クエリ「SELECT dbo.emp_leave_sub(lv,dr) FROM Employee1 where Employee1.emp_id='」+ emp_id + 「'」の代わりに「SELECT dbo.emp_leave_sub(lv,dr) as rest」を試しましたが、そうではありませんでした仕事。私が間違っていることを理解できません。

親切な返信をお待ちしております。どんな助けでも大歓迎です。

以下は私の機能です:

    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 に答える