-4

i have a function (not a stored procedure) in DB which returns 0/1. How can I retrieve this value in C#?

ALTER function dbo.code
(@_code  INT)
RETURNS INT
AS
BEGIN
DECLARE @result bt
    IF EXISTS (
           SELECT fld_code
           FROM   client.TBL_Table(NOLOCK)
           WHERE  code = @_code
                  AND used = 0
       )
        SET @result = 1
    ELSE
        SET @result = 0

    RETURN @result
END
Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
Paradigm
  • 193
  • 1
  • 4
  • 11
  • @walther - hello, this is what happens when stressed at hurried jobs and wanted to get a quicker answer when you are hurried up. I have waited for the searcher to show me something before I posted, but somehow it didn't, so...here we are. Pls don't down, I really need this account. Thanks – Paradigm Jun 06 '13 at 20:55
  • @JMK - hello, this is what happens when stressed at hurried jobs and wanted to get a quicker answer when you are hurried up. I have waited for the searcher to show me something before I posted, but somehow it didn't, so...here we are. Pls don't down, I really need this account. Thanks – Paradigm Jun 09 '13 at 10:46

2 Answers2

2

Create a command with the following query

SELECT [dbo].[code] as No

Then use the SqlDataReader, to read the returned value.

Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38
2

This will get you some return value from your function. Make sure you add:

using System.Data.SqlClient 

to your document.

using (SqlConnection conn = new SqlConnection(yourConnectionString))
using (SqlCommand cmd = new (SqlCommand("SELECT [dbo].[code] as returnValue", conn))
{
    conn.open();
    var returnValue = cmd.ExecuteReader() as int;
    //Do something with your value
}

For more information on SqlConnection strings, check SQL Connection Strings

Evan L
  • 3,805
  • 1
  • 22
  • 31