CodeSmith Community
Your Code. Your Way. Faster!

Generate Typed DataTable from Stored Procedure

Latest post 01-03-2008 3:26 PM by Khaos. 6 replies.
  • 01-02-2008 3:42 PM

    • rferrill
    • Not Ranked
    • Joined on 10-09-2006
    • Posts 5
    • Points 175

    Generate Typed DataTable from Stored Procedure

    I am a brand new CodeSmith user. I have a stored procedure that selects rows for custom business entity. I would like to create a CodeSmith template that has the stored procedure as a parameter and generates a typed DataTable class for the entity. Is there a way that I can analyze the stored procedure and/or its output in order to determine column names and perhaps even attributes of the columns in source tables? I know that Visual Studio will generate a typed DataSet from a stored procedure. I am attempting to do something similar, only with the classes designed the way I want them instead of the way Visual Studio constructs them. Can somebody point me in the right direction? Thanks.

    • Post Points: 35
  • 01-02-2008 4:04 PM In reply to

    • Khaos
    • Top 75 Contributor
    • Joined on 06-03-2005
    • CodeSmith Padawan
    • Posts 62
    • Points 1,026

    Re: Generate Typed DataTable from Stored Procedure

    You should look at the DBDocumenter example.  Specifically in sproc_details.cst you can see where Eric parses the SPROC and gets parameter details.

    HTH,

    Joe Johnston
    • Post Points: 35
  • 01-02-2008 4:32 PM In reply to

    • shannon
    • Top 50 Contributor
    • Joined on 03-06-2007
    • Posts 73
    • Points 5,350

    Re: Generate Typed DataTable from Stored Procedure

     There is also the CommandWrapper Template samples in the same folder.  The CommandWrapper template looks at a stored procedure and creates an object wrapper to execute the stored procedure.

    • Post Points: 35
  • 01-03-2008 6:02 AM In reply to

    • rferrill
    • Not Ranked
    • Joined on 10-09-2006
    • Posts 5
    • Points 175

    Re: Generate Typed DataTable from Stored Procedure

    Thanks for the suggestions. I've looked at these, but I need information about the table data that a stored procedure returns. These examples deal more with the input and output parameters. I've been exploring the CommandResultSchema with some success, but I'm having trouble determining whether or not a CommandResultColumnSchema represents an IDENTITY column in the source table. Do you have any thoughts on this? Thanks.

    • Post Points: 35
  • 01-03-2008 10:30 AM In reply to

    • Khaos
    • Top 75 Contributor
    • Joined on 06-03-2005
    • CodeSmith Padawan
    • Posts 62
    • Points 1,026

    Re: Generate Typed DataTable from Stored Procedure

    I understand what you are saying now.  This doesn't exist yet as far as I know.  However there is no reason why you couldn't write a template to actually parse the SPROCs to determine the source tables attributes.  The table details template will allow the harvesting of this info.  I know this may seem rudimentary BUT it would be complete information.

    <% if (SourceTable.Columns[x].IsPrimaryKeyMember) { %>
    <% if (SourceTable.Columns[x].ExtendedProperties["CS_IsIdentity"].Value.ToString().ToLower() == "true") { %>

    I hope this helps,

    Joe Johnston
    • Post Points: 35
  • 01-03-2008 3:18 PM In reply to

    • rferrill
    • Not Ranked
    • Joined on 10-09-2006
    • Posts 5
    • Points 175

    Re: Generate Typed DataTable from Stored Procedure

    Yes, it looks like that is what I will need to do. I'll need to determine the mapping of result columns to database table columns. Once I've got that, determining whether or not a source table column is an identity column appears to be trivial. Do you know of any code that attempts to map stored procedure result columns to source database columns?

    • Post Points: 35
  • 01-03-2008 3:26 PM In reply to

    • Khaos
    • Top 75 Contributor
    • Joined on 06-03-2005
    • CodeSmith Padawan
    • Posts 62
    • Points 1,026

    Re: Generate Typed DataTable from Stored Procedure

    No, I haven't seen that already done. Sad  Everything I have seen starts with the table and moves out.

    Sorry,

    Joe Johnston
    • Post Points: 5
Page 1 of 1 (7 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems