SQLDataReader and the length of a string field

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
 
C

cfps.Christian

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.
 
D

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.

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.
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top