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#?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.