Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

reading parameters in C#

  • 19-02-2004 4: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: 684 ✭✭✭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