PC Review


Reply
Thread Tools Rate Thread

Computed Columns

 
 
Aamir Mahmood
Guest
Posts: n/a
 
      23rd Feb 2007
Hi All

I have DataTable object. Is there a way that I can know which fields
(columns) in the table are computed.

Apparantly the DataTable.Columns returns all columns both computed and
other.

Any help would be appreciated.

Thanks.

Amir


 
Reply With Quote
 
 
 
 
mangist@gmail.com
Guest
Posts: n/a
 
      23rd Feb 2007
On Feb 22, 7:28 pm, "Aamir Mahmood" <aba....@efg.hij> wrote:
> Hi All
>
> I have DataTable object. Is there a way that I can know which fields
> (columns) in the table are computed.
>
> Apparantly the DataTable.Columns returns all columns both computed and
> other.
>
> Any help would be appreciated.
>
> Thanks.
>
> Amir


Check the DataColumn.Expression property. You can add computed
columns to a DataTable like this:

myDataTable.Columns.Add (new DataColumn ("ComputedValue", typeof
(decimal), "Qty * Price");

The "Qty * Price" expression will calculate the Qty column multiplied
by the Price column.

Hope this helps.

 
Reply With Quote
 
Aamir Mahmood
Guest
Posts: n/a
 
      23rd Feb 2007
The computed column is already in database. I just want to know in my
program whether this column is a computed field or not.

The Expression property is always empty string for both computed and regular
columns.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 22, 7:28 pm, "Aamir Mahmood" <aba....@efg.hij> wrote:
>> Hi All
>>
>> I have DataTable object. Is there a way that I can know which fields
>> (columns) in the table are computed.
>>
>> Apparantly the DataTable.Columns returns all columns both computed and
>> other.
>>
>> Any help would be appreciated.
>>
>> Thanks.
>>
>> Amir

>
> Check the DataColumn.Expression property. You can add computed
> columns to a DataTable like this:
>
> myDataTable.Columns.Add (new DataColumn ("ComputedValue", typeof
> (decimal), "Qty * Price");
>
> The "Qty * Price" expression will calculate the Qty column multiplied
> by the Price column.
>
> Hope this helps.
>



 
Reply With Quote
 
=?ISO-8859-2?Q?S=B3awek?=
Guest
Posts: n/a
 
      23rd Feb 2007
Aamir Mahmood wrote:
> The computed column is already in database. I just want to know in my
> program whether this column is a computed field or not.
>
> The Expression property is always empty string for both computed and regular
> columns.


When you receive data from database, you will never receive them as a
computed data. Generally computed data could came from views only, so
check the definition of view if you want to know it is a computed field
or not. From DataTable you can not grab such information, it is too late.

best,
Slawomir
 
Reply With Quote
 
Aamir Mahmood
Guest
Posts: n/a
 
      23rd Feb 2007
So, you are saying that there is no way to know whether the column is
computed or not?

If DataTable does not give me such information, I am open to any solution,
including querying the dabatase for schema.

Thanks.


"Sławek" <(E-Mail Removed)> wrote in message
news:ernk9u$feu$(E-Mail Removed)...
> Aamir Mahmood wrote:
>> The computed column is already in database. I just want to know in my
>> program whether this column is a computed field or not.
>>
>> The Expression property is always empty string for both computed and
>> regular columns.

>
> When you receive data from database, you will never receive them as a
> computed data. Generally computed data could came from views only, so
> check the definition of view if you want to know it is a computed field or
> not. From DataTable you can not grab such information, it is too late.
>
> best,
> Slawomir



 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      24th Feb 2007
I can give you some code that will query a SQLServer database for schema
information, but it's in VB. Do you want me to post it?

Robin S.
---------------------------------
"Aamir Mahmood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> So, you are saying that there is no way to know whether the column is
> computed or not?
>
> If DataTable does not give me such information, I am open to any
> solution, including querying the dabatase for schema.
>
> Thanks.
>
>
> "Sławek" <(E-Mail Removed)> wrote in message
> news:ernk9u$feu$(E-Mail Removed)...
>> Aamir Mahmood wrote:
>>> The computed column is already in database. I just want to know in my
>>> program whether this column is a computed field or not.
>>>
>>> The Expression property is always empty string for both computed and
>>> regular columns.

>>
>> When you receive data from database, you will never receive them as a
>> computed data. Generally computed data could came from views only, so
>> check the definition of view if you want to know it is a computed field
>> or not. From DataTable you can not grab such information, it is too
>> late.
>>
>> best,
>> Slawomir

>
>



 
Reply With Quote
 
Aamir Mahmood
Guest
Posts: n/a
 
      24th Feb 2007
I would really appreciate it, thanks.

But does it really tell about which columns are computed in a given table?

Please post the code, I will look into it.

-
Aamir.


"RobinS" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I can give you some code that will query a SQLServer database for schema
>information, but it's in VB. Do you want me to post it?
>
> Robin S.
> ---------------------------------
> "Aamir Mahmood" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> So, you are saying that there is no way to know whether the column is
>> computed or not?
>>
>> If DataTable does not give me such information, I am open to any
>> solution, including querying the dabatase for schema.
>>
>> Thanks.
>>
>>
>> "Sławek" <(E-Mail Removed)> wrote in message
>> news:ernk9u$feu$(E-Mail Removed)...
>>> Aamir Mahmood wrote:
>>>> The computed column is already in database. I just want to know in my
>>>> program whether this column is a computed field or not.
>>>>
>>>> The Expression property is always empty string for both computed and
>>>> regular columns.
>>>
>>> When you receive data from database, you will never receive them as a
>>> computed data. Generally computed data could came from views only, so
>>> check the definition of view if you want to know it is a computed field
>>> or not. From DataTable you can not grab such information, it is too
>>> late.
>>>
>>> best,
>>> Slawomir

>>
>>

>
>



 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      26th Feb 2007

Here are all of the properties (and their types) you can retrieve for the
data columns using a DataReader.

col name = ColumnName, type = System.String
col name = ColumnOrdinal, type = System.Int32
col name = ColumnSize, type = System.Int32
col name = NumericPrecision, type = System.Int16
col name = NumericScale, type = System.Int16
col name = IsUnique, type = System.Boolean
col name = IsKey, type = System.Boolean
col name = BaseServerName, type = System.String
col name = BaseCatalogName, type = System.String
col name = BaseColumnName, type = System.String
col name = BaseSchemaName, type = System.String
col name = BaseTableName, type = System.String
col name = DataType, type = System.Type
col name = AllowDBNull, type = System.Boolean
col name = ProviderType, type = System.Int32
col name = IsAliased, type = System.Boolean
col name = IsExpression, type = System.Boolean
col name = IsIdentity, type = System.Boolean
col name = IsAutoIncrement, type = System.Boolean
col name = IsRowVersion, type = System.Boolean
col name = IsHidden, type = System.Boolean
col name = IsLong, type = System.Boolean
col name = IsReadOnly, type = System.Boolean
col name = ProviderSpecificDataType, type = System.Type
col name = DataTypeName, type = System.String
col name = XmlSchemaCollectionDatabase, type = System.String
col name = XmlSchemaCollectionOwningSchema, type = System.String
col name = XmlSchemaCollectionName, type = System.String
col name = UdtAssemblyQualifiedName, type = System.String
col name = NonVersionedProviderType, type = System.Int32

About halfway down the list you will find [IsExpression]. This is probably
what you are looking for.

Here's how I got this list; tableName is passed in as a String. This shows
the columns you can get, and then shows selected values for each column
defined in the table.

I am using this methodology to create a stored procedure generator for CRUD
sprocs, because I'm tired of writing them myself! I use this to find out
which fields are keys, if they are identity columns, and the data types and
field sizes.

Dim cn As New SqlConnection(My.Settings.DBConnString)
'put the table name in brackets in case it has spaces in it
Dim SQLString As String = "SELECT * FROM [" & tableName & "]"
Try
cn.Open()
Dim cmd As New SqlCommand(SQLString, cn)
Dim rdr As SqlDataReader =
cmd.ExecuteReader(CommandBehavior.KeyInfo)
Dim tbl As DataTable = rdr.GetSchemaTable
'This shows all of the information you can access about each
column.
For Each col As DataColumn In tbl.Columns
Debug.Print("col name = " & col.ColumnName & _
", type = " & col.DataType.ToString)
Next
For Each row As DataRow In tbl.Rows
'DataTypeName actually gives the same
' data type name as is displayed in SQLServer
Debug.Print("{0}, ColumnSize = {1}, DataType = {2},
DataTypeName = {3}, IsExpression = {4} ", _
row("ColumnName"), row("ColumnSize"), row("DataType"), _
row("DataTypeName"), row("IsExpression"))
Next
rdr.Close()
Catch
MessageBox.Show("Error opening the connection to the database.")
Finally
cn.Close()
End Try

If [IsExpression] doesn't do it, check IsReadOnly (because you're think
that a calculated column would be read-only), or some of the other columns.

Trial and error (or, as Microsoft calls it, "iterative experimentation") is
your best friend.

Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
"Aamir Mahmood" <(E-Mail Removed)> wrote in message
news:Occ67I$(E-Mail Removed)...
>I would really appreciate it, thanks.
>
> But does it really tell about which columns are computed in a given
> table?
>
> Please post the code, I will look into it.
>
> -
> Aamir.
>
>
> "RobinS" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I can give you some code that will query a SQLServer database for schema
>>information, but it's in VB. Do you want me to post it?
>>
>> Robin S.
>> ---------------------------------
>> "Aamir Mahmood" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> So, you are saying that there is no way to know whether the column is
>>> computed or not?
>>>
>>> If DataTable does not give me such information, I am open to any
>>> solution, including querying the dabatase for schema.
>>>
>>> Thanks.
>>>
>>>
>>> "Sławek" <(E-Mail Removed)> wrote in message
>>> news:ernk9u$feu$(E-Mail Removed)...
>>>> Aamir Mahmood wrote:
>>>>> The computed column is already in database. I just want to know in
>>>>> my program whether this column is a computed field or not.
>>>>>
>>>>> The Expression property is always empty string for both computed and
>>>>> regular columns.
>>>>
>>>> When you receive data from database, you will never receive them as a
>>>> computed data. Generally computed data could came from views only, so
>>>> check the definition of view if you want to know it is a computed
>>>> field or not. From DataTable you can not grab such information, it is
>>>> too late.
>>>>
>>>> best,
>>>> Slawomir
>>>
>>>

>>
>>

>
>



 
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
Computed Columns Aamir Mahmood Microsoft ADO .NET 9 26th Feb 2007 12:49 AM
DataReader and computed columns am72de@yahoo.de Microsoft C# .NET 0 20th Feb 2007 03:09 PM
Computed columns in datagrid Raymond Du Microsoft ASP .NET 3 2nd Dec 2004 05:32 AM
Computed columns on Remoting scenario Rui Rosa Mendes Microsoft ADO .NET 0 18th Mar 2004 05:52 PM
Computed Columns in Access Luis Garcia Microsoft Access Queries 1 3rd Jan 2004 02:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:37 PM.