J
Jamin Mace
I am trying to pull data out of a data reader by column name from a cmd that
joins to tables. Both tables have an "ID" column. How do I access by column
name and include the table to access from? All of my code and sql is below
I want to access the data this way to that I don't have to alias the columns
in all of my queries.
Thanks in advance
Jamin Mace
Code:
Dim cn As SqlConnection
Dim dr As SqlDataReader
Dim cmd As SqlCommand
cn = New SqlConnection("ConnectionString")
cn.Open()
cmd = New SqlCommand("SELECT dbo.temp1.ID, " & _
"dbo.temp1.temp1Data, " & _
"dbo.temp2.ID, " & _
"dbo.temp2.temp2data," & _
"FROM(dbo.temp1) " & _
"LEFT JOIN dbo.temp2 on dbo.temp1.temp2id = dbo.temp2.id ")
cmd.Connection = cn
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
'try to extract data using columnname
Debug.WriteLine(dr.Item("ID")) 'This prints the temp1.ID How do I print the
temp2.ID
Debug.WriteLine(dr.Item("dbo.temp2.ID")) 'This gives an index out of range
property
'How to I access the temp.ID and temp2.ID using the column name since the
columns have the same name
Here is my test SQL Scripts:
/*Table scripts */
CREATE TABLE [dbo].[temp1] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[temp1Data] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[temp2ID] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[temp2] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[temp2data] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[temp1] WITH NOCHECK ADD
CONSTRAINT [PK_temp1] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[temp2] WITH NOCHECK ADD
CONSTRAINT [PK_temp2] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[temp1] ADD
CONSTRAINT [FK_temp1_temp2] FOREIGN KEY
(
[temp2ID]
) REFERENCES [dbo].[temp2] (
[ID]
)
GO
/*Insert some test data */
INSERT dbo.temp2
(temp2Data)
Values ('TestTemp2Data')
INSERT dbo.temp2
(temp2Data)
Values ('TestTemp2Data1')
INSERT dbo.temp1
(temp1data,
temp2ID)
Values ('testTemp1Data',
1)
/* now select the data */
SELECT dbo.temp1.ID,
dbo.temp1.temp1Data,
dbo.temp2.ID,
dbo.temp2.temp2data
FROM dbo.temp1
LEFT JOIN dbo.temp2 on dbo.temp1.temp2id = dbo.temp2.id
joins to tables. Both tables have an "ID" column. How do I access by column
name and include the table to access from? All of my code and sql is below
I want to access the data this way to that I don't have to alias the columns
in all of my queries.
Thanks in advance
Jamin Mace
Code:
Dim cn As SqlConnection
Dim dr As SqlDataReader
Dim cmd As SqlCommand
cn = New SqlConnection("ConnectionString")
cn.Open()
cmd = New SqlCommand("SELECT dbo.temp1.ID, " & _
"dbo.temp1.temp1Data, " & _
"dbo.temp2.ID, " & _
"dbo.temp2.temp2data," & _
"FROM(dbo.temp1) " & _
"LEFT JOIN dbo.temp2 on dbo.temp1.temp2id = dbo.temp2.id ")
cmd.Connection = cn
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
'try to extract data using columnname
Debug.WriteLine(dr.Item("ID")) 'This prints the temp1.ID How do I print the
temp2.ID
Debug.WriteLine(dr.Item("dbo.temp2.ID")) 'This gives an index out of range
property
'How to I access the temp.ID and temp2.ID using the column name since the
columns have the same name
Here is my test SQL Scripts:
/*Table scripts */
CREATE TABLE [dbo].[temp1] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[temp1Data] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[temp2ID] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[temp2] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[temp2data] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[temp1] WITH NOCHECK ADD
CONSTRAINT [PK_temp1] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[temp2] WITH NOCHECK ADD
CONSTRAINT [PK_temp2] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[temp1] ADD
CONSTRAINT [FK_temp1_temp2] FOREIGN KEY
(
[temp2ID]
) REFERENCES [dbo].[temp2] (
[ID]
)
GO
/*Insert some test data */
INSERT dbo.temp2
(temp2Data)
Values ('TestTemp2Data')
INSERT dbo.temp2
(temp2Data)
Values ('TestTemp2Data1')
INSERT dbo.temp1
(temp1data,
temp2ID)
Values ('testTemp1Data',
1)
/* now select the data */
SELECT dbo.temp1.ID,
dbo.temp1.temp1Data,
dbo.temp2.ID,
dbo.temp2.temp2data
FROM dbo.temp1
LEFT JOIN dbo.temp2 on dbo.temp1.temp2id = dbo.temp2.id