473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Computed Columns

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
Feb 23 '07 #1
7 5739
On Feb 22, 7:28 pm, "Aamir Mahmood" <aba....@efg.hijwrote:
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.

Feb 23 '07 #2
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.
<ma*****@gmail.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
On Feb 22, 7:28 pm, "Aamir Mahmood" <aba....@efg.hijwrote:
>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.

Feb 23 '07 #3
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
Feb 23 '07 #4
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" <sp****************@o2.plwrote in message
news:er**********@node1.news.atman.pl...
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

Feb 23 '07 #5
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" <ab*****@efg.hijwrote in message
news:et**************@TK2MSFTNGP02.phx.gbl...
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" <sp****************@o2.plwrote in message
news:er**********@node1.news.atman.pl...
>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


Feb 24 '07 #6
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" <Ro****@NoSpam.yah.nonewrote in message
news:rJ******************************@comcast.com. ..
>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" <ab*****@efg.hijwrote in message
news:et**************@TK2MSFTNGP02.phx.gbl...
>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" <sp****************@o2.plwrote in message
news:er**********@node1.news.atman.pl...
>>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



Feb 24 '07 #7

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" <ab*****@efg.hijwrote in message
news:Oc*************@TK2MSFTNGP06.phx.gbl...
>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" <Ro****@NoSpam.yah.nonewrote in message
news:rJ******************************@comcast.com. ..
>>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" <ab*****@efg.hijwrote in message
news:et**************@TK2MSFTNGP02.phx.gbl...
>>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" <sp****************@o2.plwrote in message
news:er**********@node1.news.atman.pl...
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




Feb 26 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paulo Andre Ortega Ribeiro | last post by:
I have a table with fields called fname (First Name) and lname (Last Name). I need the user´s email thai is compose from lname and fname: LOWER(LEFT (fname,1) + lname) Is there any difference...
2
by: tperovic | last post by:
Using SS2K, I'm getting the following error while bulk inserting: Column 'warranty_expiration_date' cannot be modified because it is a computed column. Here is my bulk insert statement: ...
9
by: DMAC | last post by:
If i want to split a computed column into two or more columns based on the the length (its a varchar) of the computed column, how often will sql server determine what the computed column is?...
1
by: GJK | last post by:
In SQL Sever, do the size of computed columns gets added to the total size of the tables? Does SQL server stores the actual values in computed columns? Thanks _GJK
4
by: Henning N?rg?rd | last post by:
I'm working as software developer mostely om SQL-server platform. On SQL-server we are using a lot of "Computed Columns" Does anyone know if that is possible to do in DB2 too ? I mean - define...
0
by: Jim Heavey | last post by:
Hello, I have created a computed column which concatenates a name and date. My problem is that if the 10 is not 10 characters, I get an extra character placed into the computed column. Here is the...
3
by: Raymond Du | last post by:
Hi, Can I have computed columns in a datagrid? If yes, how? TIA
1
by: Dave | last post by:
I am relativly new to visual basic, so this may be a no- brainer. I am attempting to use a computed column in a VB dataset defined as followe: ' 'dcCost ' Me.dcCost.ColumnName = "Cost"
0
by: am72de | last post by:
Hi all, I have two identical DataTables. One of the columns is a computed one. With a DataReader I try to copy the DataRows from one table to the other, but I get an InvalidOperationException,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.