Re: Joining two tables with fields that have the same names
- From: v-wywang@xxxxxxxxxxxxxxxxxxxx (WenYuan Wang [MSFT])
- Date: Thu, 24 May 2007 10:37:39 GMT
Hello Monty,
Actually, in ADO.net world, there is a way to get row by specifying
"TableName.FieldName". We need do coding manually if you truly want to
achieve this.
You may execute sqldatacommand with "CommandBehavior.KeyInfo" to get
SchemaTable from the result, and then search the ordinal by specifying
TableName and ColumnName.
Such as below.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(...);
System.Data.SqlClient.SqlCommand scd = new
System.Data.SqlClient.SqlCommand(...,cn);
cn.Open();
System.Data.SqlClient.SqlDataReader
sdr=scd.ExecuteReader(System.Data.CommandBehavior.KeyInfo);
while (sdr.Read())
{
getObjectByTableNameAndColumn(sdr,"TableName","ColumnName")
}
public object
getObjectByTableNameAndColumn(System.Data.SqlClient.SqlDataReader sdr,
string tableName, string columnName)
{
string filterString=string.Format("basecolumnname='{0}'and
basetablename='{1}'", columnName, tableName);
int ordinal =
(int)sdr.GetSchemaTable().Select(filterString)[0]["ColumnOrdinal"];
return sdr[ordinal];
}
This method only works for tableName.
Such as
getObjectByTableNameAndColumn(sdr," ParentTable","ColumnName") is valid.
getObjectByTableNameAndColumn(sdr,"P","ColumnName") is not invalid.
Hope I have clarified it for you. Please let me know if you still have
anything unclear.
Have a great day. Thanks.
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- Follow-Ups:
- References:
- Joining two tables with fields that have the same names
- From: Monty
- Re: Joining two tables with fields that have the same names
- From: Bob Barrows [MVP]
- Re: Joining two tables with fields that have the same names
- From: Monty
- Joining two tables with fields that have the same names
- Prev by Date: Re: Joining two tables with fields that have the same names
- Next by Date: Re: Cancel query
- Previous by thread: Re: Joining two tables with fields that have the same names
- Next by thread: Re: Joining two tables with fields that have the same names
- Index(es):
Relevant Pages
|
|