PC Review


Reply
Thread Tools Rate Thread

DataTable Schema...not enough information

 
 
Josh
Guest
Posts: n/a
 
      17th Aug 2005
I need to know if there is a way to determine the precision and scale of a
column in a DataTable, when the data type is a decimal. We do not have a
problem if there is a row that has a non-null value in it using the GetBits
method. However, if the value is Null or even 0 I cannot get the scale.
Let me run through it a little bit explaining along the way. We populate
our DataSet using the following procedure:



Private Function FillDataset(ByVal voCommand As SqlCommand, _
ByVal veMissingSchemaAction As
MissingSchemaAction) As DataSet

Dim loDA As SqlDataAdapter
Dim loDS As DataSet

Try
loDA = New SqlDataAdapter(voCommand)
loDS = New DataSet

'MissingSchemaAction can now be set when retrieving dataset. This
was necessary
'for proper sorting when going through trsADO interface.

loDA.MissingSchemaAction = veMissingSchemaAction
loDA.Fill(loDS)

Return loDS

Finally
CleanUpDatabaseObjs(roDataAdapter:=loDA)

End Try

End Function

In this case we are using MissingSchemaAction of Add. Let just say for this
command we have 1 table, with 2 columns and 3 rows. The columns are defined
in SQL server as a Decimal(5,3) and the values are as follows:
Column1 Column2
------------------- ---------------
Null 23.12
12.023 Null
1.555 0

Column2 is not a problem for me, because I can look at the value in the
first row and using the Decimal.GetBits method I can determine the scale
(believe it or not the scale correctly returns 3). However, I cannot do a
GetBits on Row 1 of Column1 because the value is Null. Furthermore I
cannot spin through all the rows until I find a non-null/non-zero value
because first we use some pretty big tables with many rows, performance is
an issue and second, and probably more importantly, we cannot guarantee that
we will always have a value, sometimes the entire column could be filled
with Null or 0.

So, is there a way to tap into the underlying table schema and determine how
a column is defined? I am actually kind of surprised that there are not
properties on the columns collection for precision and scale, but there is
not, so I need another solution to figure it out.

Thanks!


 
Reply With Quote
 
 
 
 
Mark Ashton
Guest
Posts: n/a
 
      18th Aug 2005
You don't have access to that information via fill, but it does exist.
// MissingSchemaAction.AddWithKey uses CommandBehavior.KeyInfo

SqlDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo)
DataTable table = reader.GetSchemaTable() ' this table has the information
' The "NumericPrecision" and "NumericScale" columns have that information.

Unfortunately if you get the information this way, its not obvious to then
fill the DataSet without rexecuting.

(new LoadDataReader()).Fill(dataset, reader);

internal class LoadDataReader : System.Data.Common.DbDataAdapter() {
internal int Fill(DataSet dataset, IDataReader reader) {
// the method on DbDataAdapter that takes a DataTable and a reader
is protected
// so you can only call it if your derive from DbDataAdapter and
delegate to it
base.Fill(table, "Table", reader, 0, 0);
}
}

--
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.

"Josh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I need to know if there is a way to determine the precision and scale of a
>column in a DataTable, when the data type is a decimal. We do not have a
>problem if there is a row that has a non-null value in it using the GetBits
>method. However, if the value is Null or even 0 I cannot get the scale.
>Let me run through it a little bit explaining along the way. We populate
>our DataSet using the following procedure:
>
>
>
> Private Function FillDataset(ByVal voCommand As SqlCommand, _
> ByVal veMissingSchemaAction As
> MissingSchemaAction) As DataSet
>
> Dim loDA As SqlDataAdapter
> Dim loDS As DataSet
>
> Try
> loDA = New SqlDataAdapter(voCommand)
> loDS = New DataSet
>
> 'MissingSchemaAction can now be set when retrieving dataset. This
> was necessary
> 'for proper sorting when going through trsADO interface.
>
> loDA.MissingSchemaAction = veMissingSchemaAction
> loDA.Fill(loDS)
>
> Return loDS
>
> Finally
> CleanUpDatabaseObjs(roDataAdapter:=loDA)
>
> End Try
>
> End Function
>
> In this case we are using MissingSchemaAction of Add. Let just say for
> this command we have 1 table, with 2 columns and 3 rows. The columns are
> defined in SQL server as a Decimal(5,3) and the values are as follows:
> Column1 Column2
> ------------------- ---------------
> Null 23.12
> 12.023 Null
> 1.555 0
>
> Column2 is not a problem for me, because I can look at the value in the
> first row and using the Decimal.GetBits method I can determine the scale
> (believe it or not the scale correctly returns 3). However, I cannot do a
> GetBits on Row 1 of Column1 because the value is Null. Furthermore I
> cannot spin through all the rows until I find a non-null/non-zero value
> because first we use some pretty big tables with many rows, performance is
> an issue and second, and probably more importantly, we cannot guarantee
> that we will always have a value, sometimes the entire column could be
> filled with Null or 0.
>
> So, is there a way to tap into the underlying table schema and determine
> how a column is defined? I am actually kind of surprised that there are
> not properties on the columns collection for precision and scale, but
> there is not, so I need another solution to figure it out.
>
> Thanks!
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using DataAdapter.Fill to return a DataTable complete with schema information \(O\)enone Microsoft ADO .NET 2 1st Aug 2007 12:49 PM
Using DataAdapter.Fill to return a DataTable complete with schema information \(O\)enone Microsoft VB .NET 2 1st Aug 2007 12:49 PM
Append 1 Datatable to another (same schema)?? SLN Microsoft ADO .NET 1 4th Dec 2003 07:16 PM
DataTable schema changes Thomas Brown Microsoft ADO .NET 2 17th Sep 2003 05:55 PM
DataTable schema for identity joe Microsoft ADO .NET 1 15th Jul 2003 04:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:19 PM.