Re: Joining two tables with fields that have the same names



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.

.



Relevant Pages

  • Re: SQL 2000 CREATE FUNCTION
    ... ByVal tableName As String) As String ... If tableSchema Is Nothing OrElse tableSchema.Length = 0 Then ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)
  • Re: Dataset from XML file
    ... put together in a file called Tag. ... I create a strongly typed dataset. ... Tablename = Appr ... ColumnName = form_Id ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Dataset from XML file
    ... I am taking an xml file that is given to my by a client that is created by ... and tell which form a tag is with. ... Tablename = Appr ... ColumnName = form_Id ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Fastest way to count records in table
    ... and then pass the tablename and the fieldname to the function and use it to ... Function RecordsInTable(Tablename As String, Fieldname As String) As Long ... If you use a totals query, you don't have to bother with checking for EOF ...
    (microsoft.public.access.formscoding)
  • Re: Creating an Excel Spreadsheet from code on a computer that does not have Excel installed
    ... Private Sub CreateXLSFromDt(ByVal ds As DataSet, ByVal tablename As String, ... converted by Excel when openned ...
    (microsoft.public.dotnet.languages.vb)