Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Posts
    186

    Unanswered: Reading results into .NET

    Hi. I want to write a function to retrieve all records from a "Parts" table so that I can read these records in .NET.
    Code:
    SqlDataReader reader = sqlCmd.ExecuteReader()
    
    while( reader.Read() )
    {
        int x = reader["id"];
        string partName = reader["name"];
    
        // Do stuff with data here //
    }
    My question: How should the function be written?

    Should I create a PROCEDURE and just call a SELECT statement?
    Code:
    CREATE PROCEDURE getPartsList()
    AS
      SELECT part_id as 'id', part_name as 'name' FROM parts
    Or should I create a FUNCTION that returns a TABLE? If so, is this the correct syntax?
    Code:
    CREATE FUNCTION getPartsList()
    RETURNS TABLE
    AS
      RETURN (SELECT part_id as 'id', part_name as 'name' FROM parts)
    Thanks for reading

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Code:
    CREATE PROCEDURE getPartsList
    AS
      SELECT part_id as 'id', part_name as 'name' FROM parts
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jan 2008
    Posts
    186
    Ahh okay. So it's not necessary to create FUNCTION that returns a type TABLE?

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    correct












    .
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •