SQLDataReader and the length of a string field

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

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
 
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.
 
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.
 
Dom said:
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,CHARACTER_MAXIMUM_LENGTH FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=@tblnam

should get it.

Arne
 
Back
Top