I recent found myself using the SqlDataReader.GetSchemaTable method. I made some notes detailing what it will return for various data types in SQL Server 2005.
Numeric | |||||
---|---|---|---|---|---|
ColumnName | DataType | ColumnSize | Precision | Scale | Notes |
bigint | Int64 | 8 | 19 | 255 | |
bit | Boolean | 1 | 255 | 255 | |
decimal | Decimal | 17 | 18 | 0 | |
int | Int32 | 4 | 10 | 255 | |
money | Decimal | 8 | 19 | 255 | |
numeric | Decimal | 17 | 18 | 0 | |
smallint | Int16 | 2 | 5 | 255 | |
smallmoney | Decimal | 4 | 10 | 255 | |
tinyint | Byte | 1 | 3 | 255 | |
float | Double | 8 | 15 | 255 | |
real | Single | 4 | 7 | 255 | |
datetime | DateTime | 8 | 23 | 3 | |
smalldatetime | DateTime | 4 | 16 | 0 | |
char | String | 20 | 255 | 255 | |
varchar | String | 20 | 255 | 255 | |
text | String | 2147483647 | 255 | 255 | isLong |
nchar | String | 20 | 255 | 255 | |
nvarchar | String | 20 | 255 | 255 | |
varchar(max) | String | 2147483647 | 255 | 255 | isLong |
nvarchar(max) | String | 2147483647 | 255 | 255 | isLong |
ntext | String | 1073741823 | 255 | 255 | isLong |
timestamp | Byte[] | 8 | 255 | 255 | IsRowVersion |
xml | String | 2147483647 | 255 | 255 | isLong |
I created these notes through the simple method of creating a table with every possible data type. Or rather, every data type that I am interested in, which is very nearly the same thing--I mean, who uses varbinary, really? I then used the GetSchemaTable method on my new table, and inspected the results.
There is a page on MSDN that appears to contain this information, but upon closer inspection, it must be talking about something else entirely. For instance, it claims that the SQL Server data type "varchar" has no mapping whatsoever, not even as a System.String.
Certainly, this makes sense upon further reflection: it's possible to put any varchar value into a string, but not the other way around. Still, it's not terribly useful if you just want to know what sort of data type you can reasonably expect to get out of a given column.
I further note that the MSDN documentation states that the NumericPrecision column should be null for non-numeric data types, but this is simply not true.
No comments:
Post a Comment