Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

reading parameters in C#

  • 19-02-2004 04:00PM
    #1
    Registered Users, Registered Users 2 Posts: 2,191 ✭✭✭


    anybody know how to read out parameters from a stored procedure in C#?

    I can write the procedures no problem its just the code on the C# side thats causing me problems


Comments

  • Registered Users, Registered Users 2 Posts: 688 ✭✭✭JazzyJ


    Something to this effect:


    SqlConnection cnPubs = new SqlConnection("server=localhost;integrated security=true;database=pubs");

    SqlCommand cmdAuthors = new SqlCommand("up_AuthorBookCount", cnPubs);

    cmdAuthors.CommandType = CommandType.StoredProcedure;

    cmdAuthors.Parameters.Add("@au_id", SqlDbType.NVarChar, 11);
    cmdAuthors.Parameters["@au_id"].Value = AuthorID;

    cmdAuthors.Parameters.Add("@Count", SqlDbType.Int);
    cmdAuthors.Parameters["@Count"].Direction = ParameterDirection.Output;

    cnPubs.Open();
    int iCount;
    cmdAuthors.ExecuteNonQuery();
    iCount = (int) cmdAuthors.Parameters["@Count"].Value;
    cnPubs.Close();


  • Registered Users, Registered Users 2 Posts: 2,191 ✭✭✭Unpossible



    cmdAuthors.Parameters.Add("@au_id", SqlDbType.NVarChar, 11);
    cmdAuthors.Parameters["@au_id"].Value = AuthorID;

    cmdAuthors.Parameters.Add("@Count", SqlDbType.Int);
    cmdAuthors.Parameters["@Count"].Direction = ParameterDirection.Output;



    could you please explain this piece in detail? as in whats happening


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Unpossible
    could you please explain this piece in detail? as in whats happening

    The first line of each "pair" is defining a parameter which the stored procedure needs.

    Earlier data-access libraries queried the DB and figured out what parameters were needed -how many, what they were named, what datatype, what "direction" (input, output, return val), and so on, so all you had to do was supply the values.

    However, this was actually quite poor in performance terms, so they decided it was a better approach to make the developer specifically state what parameters he wants, how many, what direction, what they're called, and so on and so forth.

    The second line of each pair is actually supplying a value for each parameter.

    jc


  • Registered Users, Registered Users 2 Posts: 2,191 ✭✭✭Unpossible


    I already know how to enter parameters using:

    myCommand.Parameters.Add(new SqlParameter("@Staff", SqlDbType.Int)).Value = i;

    this enters in the staffNo, how would i read the output, i.e. date


  • Registered Users, Registered Users 2 Posts: 2,191 ✭✭✭Unpossible


    its ok i got the parameters outputting. Do i still need a dataset if in just reading the output parameters


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 640 ✭✭✭Kernel32


    You don't need and shouldn't use a dataset if you are only interested in output parameters.

    Also if you use the vs.net IDE you don't need to do a lot of parameter setup. Use the visual sqlCommand, drag it on, set the connection, the proc name and it will go to the database and build the parameter list for you. I am amazed at how many .Net developers are not using the IDE to its full potential and end up writing twice as much code as they need to.


  • Registered Users, Registered Users 2 Posts: 2,191 ✭✭✭Unpossible


    I have to write the code, its my third year project & i have to do as little dragging and dropping as possible


Advertisement