SqlDataReader String Based Accessor

Last post 01-02-2009 3:03 PM by massis. 7 replies.

Sort Posts:

  • SqlDataReader String Based Accessor

    12-30-2008, 3:07 PM
    • Member
      13 point Member
    • massis
    • Member since 03-31-2008, 9:31 PM
    • Posts 26

    I'm trying to reference identically named columns in joined tables via SqlDataReader.  I have a situation similar to the following example. 

     

    ********************************************************************

    Table T1
    Columns: id, count, price, instructor, room, hours 

     

    Table T2
    Columns id, count, price, pages, required_flag

     

    Table T3
    Columns id, pages, optional 

     

    SELECT * FROM  T1
             JOIN  T2 ON T1.ID = T2.ID
             JOIN  T3 ON T1.ID = T3.ID

    *********************************************************************

    I need to use the string based SqlDataReader accessor to reference the "count' and "price" fields in T1 and T2.  Unfortunately, the "Table.Column" (ex: oReader["T1.count"], oReader["T2.count"], oReader["T3.pages"]) notation does not work.  

     

    Using the positional accessor is not feasible in this case because the tables and join order varies with each execution. 

     

    I'm hoping for a simple answer to this issue.

     

    Thanks for your help.

    Massis

      

  • Re: SqlDataReader String Based Accessor

    12-31-2008, 2:47 AM

    Use Aliases in your SQL instead of the lazy * notation:

    SELECT T1.Count AS Count1, T2.Count AS Count2.....

     

     

    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: SqlDataReader String Based Accessor

    12-31-2008, 2:20 PM
    • Member
      13 point Member
    • massis
    • Member since 03-31-2008, 9:31 PM
    • Posts 26

    Your suggestion is an excellent solution for most scenarios.  In this specific case, users construct their own SQL statements and provide a list of tables/columns they want the program to process.  Some of the transactions involve hundreds of fields so providing aliases isn't really feasible

    Is it possible to implement a function to access any given table/colum based on the schema of the returned dataset?

    Massis

     

  • Re: SqlDataReader String Based Accessor

    12-31-2008, 2:44 PM

    Since you are using a SqlDataReader (according to the title of your post), there is no DataSet.  But there is a GetName() method of the SqlDataReader. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getname.aspx.  Will that do you?

     

    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: SqlDataReader String Based Accessor

    12-31-2008, 3:14 PM
    • Member
      13 point Member
    • massis
    • Member since 03-31-2008, 9:31 PM
    • Posts 26

    The GetName() only returns the column name, so I still can't distinguish which table the column belongs to. 

    I tried using the GetSchemaTable(), hoping it returns the table/column info for each field. Unfortunatley, that information is not included in the schema table, see below.

    ************************************************************************************************ 

    ColumnName = col1

    ColumnOrdinal = 0

    ColumnSize = 50

    NumericPrecision = 255

    NumericScale = 255

    IsUnique = False

    IsKey =

    BaseServerName =

    BaseCatalogName =

    BaseColumnName = col1

    BaseSchemaName =

    BaseTableName =

    DataType = System.String

    AllowDBNull = False

    ProviderType = 22

    IsAliased =

    IsExpression =

    IsIdentity = False

    IsAutoIncrement = False

    IsRowVersion = False

    IsHidden =

    IsLong = False

    IsReadOnly = False

    ProviderSpecificDataType = System.Data.SqlTypes.SqlString

    DataTypeName = varchar

    XmlSchemaCollectionDatabase =

    XmlSchemaCollectionOwningSchema =

    XmlSchemaCollectionName =

    UdtAssemblyQualifiedName =

    NonVersionedProviderType = 22

    ************************************************************************************************

    Is there a different way I can execute the  SQL command in order to obtain the table name as part of the schema?

    Massis

     

  • Re: SqlDataReader String Based Accessor

    01-02-2009, 2:47 AM

    Hi massis,

    I'm afraid there's no other way to achieve it. When you execute the join query on database, a logic table will be returned. I think it's impossible to know which table is a certain column from. I think Mike's suggestion is extremely good although you have to change the stored procedure.

    Thanks.

    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: SqlDataReader String Based Accessor

    01-02-2009, 3:21 AM

    massis:
    Is there a different way I can execute the  SQL command in order to obtain the table name as part of the schema?
     

    Capture the query that the user creates and use that as a basis for the schema.  It would have to contain the table names, and the order in which the fields are selected. 

     

    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: SqlDataReader String Based Accessor

    01-02-2009, 3:03 PM
    Answer
    • Member
      13 point Member
    • massis
    • Member since 03-31-2008, 9:31 PM
    • Posts 26

    Writing my own lexer/parser isn't exaclty the simply solution I was hoping to find.  I'll do more research to figure out the best way to go. 

     

Page 1 of 1 (8 items)