none
How to Get the return Value of SQL2005 Function in C#2005 RRS feed

  • Question

  •  

    Helow !

     

    i have created a funciton Called GetCode in SQL2005 but i don't now how to access

     

    the return value of the function in C# 2005.

     

    Is there anybody to help me.

    Thursday, March 20, 2008 7:20 AM

Answers

  • Hello,

     

    You have to create a SqlCommand

    SqlCommand cmd = new SqlCommand();

    cmd.Connection = a SqlConnection to your database

    cmd.CommandText = "exec GetCode";

    if you have parameters for GetCode, you define the parameters @parm1 ,@parm2

    SqlParameter parm = new SqlParameter("@parm1",SqlDbType.NVarchar,50);

    parm.Value = "xxx";

    cmd.Parameters.Add(parm);

    parm = null;

    parm = new SqlParameter("@parm2",SqlDbType.Int);

    parm.Value = 50;

    cmd.Parameters.Add(parm);

    If i suppose GetCode is returning a string :

    String val = (String) cmd.ExecuteScalar();

     

    As my VC# is not available, i wrote from my memory. So i hope i haven't too many errors.

     

    ExecuteScalar is returning only one value, it would be good for you

     

    Have a nice day

     

    Thursday, March 20, 2008 9:30 AM

All replies

  • Hello,

     

    You have to create a SqlCommand

    SqlCommand cmd = new SqlCommand();

    cmd.Connection = a SqlConnection to your database

    cmd.CommandText = "exec GetCode";

    if you have parameters for GetCode, you define the parameters @parm1 ,@parm2

    SqlParameter parm = new SqlParameter("@parm1",SqlDbType.NVarchar,50);

    parm.Value = "xxx";

    cmd.Parameters.Add(parm);

    parm = null;

    parm = new SqlParameter("@parm2",SqlDbType.Int);

    parm.Value = 50;

    cmd.Parameters.Add(parm);

    If i suppose GetCode is returning a string :

    String val = (String) cmd.ExecuteScalar();

     

    As my VC# is not available, i wrote from my memory. So i hope i haven't too many errors.

     

    ExecuteScalar is returning only one value, it would be good for you

     

    Have a nice day

     

    Thursday, March 20, 2008 9:30 AM
  • Another simpler way is use "select GetCode()"

     

    Saturday, March 22, 2008 11:33 PM