Search code examples
.net.net-4.0datasetstrongly-typed-dataset

Where should I declare a general GetDataRow method in a typed DataSet?


I'm converting report code that uses a lot of hand rolled dynamic SQL to rather use dynamic datasets, but there are still many occassions for ad-hoc queries and inserts. Current code uses e.g. a GetDataRow static method in clsFunctions (yes, I know, the horror!), but I would like to relocate this function to the data layer I am establishing. To make this an extension method on my typed Table Adapters crosses my mind, because these have a defined connection property, so I know I'd always be using the same connection as other data access code, but typed table adapters have no real base class to add the method to, as they all only derive from Component, and have their 'shared' functions generated.

Where should I place my GetDataRow method, within my typed dataset based data layer?


Solution

  • Since dynamic table adapters are partial classes, simply create another partial class to add your special functions to the tableadapter.

    In this example I have a dataset called "dsMain" and a tableadapter for a "Document" table.

    namespace dsMainTableAdapters
    {
        public partial class DocumentTableAdapter
        {
            public dsMain.DocumentRow GetDataRow()
            {
                System.Data.SqlClient.SqlConnection oconn = this.Connection;
    
                //now Run your custom code on the connection
    
            }
        }
    }
    

    Now you should have a new method in your tableadapter called "GetDataRow".

    As long as you use the EXACT namespace of your dataset tableadapters and the EXACT class name for your tableadapter, you should be able to access the connection object for whatever you want.