2

Possible Duplicate:
Getting return value from stored procedure in C#

im using the following sql function in a c# winform application

create function dbo.GetLookupValue(@value INT)
returns varchar(100)
as begin
  declare @result varchar(100)

  select
    @result = somefield
  from 
    yourtable
  where 
    ID = @value;

  return @result
end

my question is: how can i read the returned @result in c#?

Community
  • 1
  • 1

1 Answers1

8

You need to use a statement like SELECT dbo.(function) to retrieve the value - something like this:

using(SqlConnection conn = new SqlConnection("server=.;database=TEST;Integrated Security=SSPI;"))
using (SqlCommand cmd = new SqlCommand("SELECT dbo.GetLookupValue(42)", conn))
{
    conn.Open();
    var result = cmd.ExecuteScalar();
    conn.Close();
}

This will execute the function and return the result value to your C# app.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • How to do the same thing if I use .edmx e.i. object context? I need to call a function that return an int value. – Badhon Jain Jul 23 '13 at 04:41