Retrieving length of Binary Column After Doing a FillSchema.  
Author Message
Darren King





PostPosted: .NET Framework Data Access and Storage, Retrieving length of Binary Column After Doing a FillSchema. Top

I'm just interested in getting the schema of a table because I want to generate an SQL Script that will create the table someplace else with just CREATE TABLE...  Now before someone suggest that I use Server Management Objects that's not the answer to this particular question.

I do this.

sqlda.SelectCommand.CommandText = "SELECT * FROM " + kingTable.Name;

sqlda.MissingSchemaAction = System.Data.MissingSchemaAction.AddWithKey;

sqlda.FillSchema( kingTable, System.Data.SchemaType.Source );

 

Then I do this.

kingTable.Columns[ "hash" ].MaxLength;

This doesn't work.  I know it shouldn't work because MaxLength is really just for strings or so it says in the documentation.  However, how do I find out what the length of this field is

In the database it's a BINARY column with a fixed size of 17.  So how do I find out that number 17 in code without using the sys.columns table

 

Thanks,



.NET Development36  
 
 
Darren King





PostPosted: .NET Framework Data Access and Storage, Retrieving length of Binary Column After Doing a FillSchema. Top

Maybe I'm supposed to be using DataType
 
 
Darren King





PostPosted: .NET Framework Data Access and Storage, Retrieving length of Binary Column After Doing a FillSchema. Top

Well I haven't been able to figure out if it's possible through a dataset. So my alternative was to create my own schema class and load in the details from the sys.tables and sys.columns views in SQL Server 2005. These tables also exist in Server 2000. So the solution could work for that too.

select * from sys.tables t INNER JOIN sys.columns c ON t.object_id = c.object_id where t.name = 'YourTable' and c.name = 'ColumnName'

From that you can determine MAX_LENGTH of the column which will give you the exact number of bytes stored in that column.