Avatar of techques
techques

asked on 

How to write VC# code to call a Functions in MSSQL 2005 server?

Hi

I write a function in MSSQL 2005 DB, how should I write the VC# code to get the return value?

Here is the C# code to get return value from a Stored Procedure.

But, how can I change it to get value from a Functions?
VC#:
public int SP_GetMemberStatus(int id)
        {
            try
            {
                using (SqlConnection Connection = new SqlConnection(ConnectionString))
                {
                    string commandstring = "SP_GetMemberStatus";
 
                    using (SqlCommand command = new SqlCommand(commandstring, Connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandTimeout = 15;    
                        command.Parameters.Add("@userid", SqlDbType.Int).Value = id;
                        command.Parameters.Add("@status", SqlDbType.Int).Direction = ParameterDirection.Output;
                        Connection.Open();
                        command.ExecuteNonQuery();
                        return Convert.ToInt32(command.Parameters["@status"].Value);
                    }
                }
            }
            catch (SqlException ex)
            {
                string e = ex.Message;
                return 0;
            }
        }

Open in new window

.NET ProgrammingMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
techques

8/22/2022 - Mon