returning dataset from stored procedure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    returning dataset from stored procedure

    Is it possible to return a dataset from a stored procedure, or would you
    need to write the SQL in your .cs file to return the dataset?


    Any assistance would be really appreciated.


    Cheers,

    Mike



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Mark Rae

    #2
    Re: returning dataset from stored procedure

    "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
    news:eyGD%23Ste EHA.1644@tk2msf tngp13.phx.gbl. ..

    Mike,
    [color=blue]
    > Is it possible to return a dataset from a stored procedure, or would you
    > need to write the SQL in your .cs file to return the dataset?[/color]

    It's actually the same thing! Supposing you have a stored proc like:

    CREATE PROC uspTestProc
    AS
    SELECT * FROM tblTest
    GO

    In your C# code, you might have a string variable called strSQL, so the
    following two statements are functionally identical:

    string strSQL = "EXEC uspTestProc";
    string strSQL = "SELECT * from tblTest";

    Of course, there are many good reasons for using stored procedures instead
    of in-line SQL. Notably, stored procedures are pre-compiled by the server
    engine so almost always execute more quickly, and they also go a long way to
    help preventing SQL injection attacks.

    Mark


    Comment

    • C Addison Ritchie

      #3
      Re: returning dataset from stored procedure

      You can't return an actual ADO.NET DataSet "object" from a stored procedure
      but that is probably not what you're asking.

      Yes you can return data from a stored procedure and have it go straight to a
      DataSet. SQL Server and Oracle both support this. IMHO I think working
      with SQL Server is easier.

      SQL Server example -----
      Create your stored procedure:
      create procedure dbo.MyStoredPro cedure
      as
      begin
      select * from Orders
      end
      GO

      Create your C# code:
      SqlCommand myCommand = new SqlCommand("dbo .MyStoredProced ure");
      myCommand.Comma ndType = CommandType.Sto redProcedure;

      // create SqlConnection
      SqlConnection myConnection = new SqlConnection(" your connection string
      here");
      myCommand.Conne ction = myConnection;
      SqlDataAdapter da = new SqlDataAdapter( myCommand);

      DataSet data = new DataSet();
      da.Fill(data);

      Oracle is a little different. If you need further help for Oracle search
      the newsgroups for "ref cursor". I think I have an example out there
      somewhere for returning data from an Oracle stored procedure.

      HTH
      --
      C Addison Ritchie, MCSD.NET
      Ritch Consulting, Inc.

      "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
      news:eyGD%23Ste EHA.1644@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Is it possible to return a dataset from a stored procedure, or would you
      > need to write the SQL in your .cs file to return the dataset?
      >
      >
      > Any assistance would be really appreciated.
      >
      >
      > Cheers,
      >
      > Mike
      >
      >
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]


      Comment

      • Mike P

        #4
        Re: returning dataset from stored procedure

        Sorry about that...I was just getting confused with output parameters
        thinking that they were the only way you can return data...as you can
        probably tell, I don't have much experience when it comes to writing
        stored procedures :)


        Cheers,

        Mike



        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        Working...