SQLDataReader and the length of a string field

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

    #1

    SQLDataReader and the length of a string field

    I use SQLDataReader to obtain data from a SQL Server backend. Within
    the program, I need to know the length of some of the varchar fields.
    Not the length of the returned value, but the maximum length, as it is
    specified in the database.

    How do I do this? I'm expecting something like this:

    SQLDataReader r = (...)
    r.GetDataLength (i)

    .... but I can't find it.

    Dom
  • cfps.Christian

    #2
    Re: SQLDataReader and the length of a string field

    Since you didn't say what this is for I'm going to make a guess and
    say that you want to dynamically create fields and limit them by the
    maxlength, or something similar to that. The only way I know to do
    this is to query the system tables which contains all that
    information. In my own code I was going to create an object that I
    could use to query the system tables in certain ways to get this
    information and similar information.

    Comment

    • Dom

      #3
      Re: SQLDataReader and the length of a string field

      On Apr 1, 4:23 pm, "cfps.Christian " <ge0193...@otc. eduwrote:
      Since you didn't say what this is for I'm going to make a guess and
      say that you want to dynamically create fields and limit them by the
      maxlength, or something similar to that.  The only way I know to do
      this is to query the system tables which contains all that
      information.  In my own code I was going to create an object that I
      could use to query the system tables in certain ways to get this
      information and similar information.
      My reason for asking is this: My program allows the user to change
      these fields, so I have to make sure that, if the field is varchar
      (50), and he enters 51 characters, the program gives him a warning.

      I take your point, though. I believe that in the days of ADO, you got
      this from the RecordSet object.

      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: SQLDataReader and the length of a string field

        Dom wrote:
        I use SQLDataReader to obtain data from a SQL Server backend. Within
        the program, I need to know the length of some of the varchar fields.
        Not the length of the returned value, but the maximum length, as it is
        specified in the database.
        I don't think you can get that info from your current DataReader.

        But:

        SELECT COLUMN_NAME,CHA RACTER_MAXIMUM_ LENGTH FROM
        INFORMATION_SCH EMA.COLUMNS WHERE TABLE_SCHEMA=@t blnam

        should get it.

        Arne

        Comment

        Working...