Oracle Database Interaction Using ODP.NET and ASP.NET: All Ways to Retrieve Data - Getting data with only a table name
(Page 3 of 5 )
Let us first go through the example first.
Dim cn As New OracleConnection("User ID=scott;password=tiger;Data
Source=ORCL")
Try
Dim cmd As New OracleCommand
cmd.CommandText = "emp"
cmd.CommandType = CommandType.TableDirect
cmd.Connection = cn
cmd.Connection.Open()
Me.DataGrid1.DataSource = cmd.ExecuteReader
Me.DataGrid1.DataBind()
cmd.Dispose()
Catch ex As Exception
Me.lblError.Text = ex.Message
Finally
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
Those who already work with ADO.NET will understand the above code within seconds. It is always a good practice to maintain exception handling in our coding, especially with Oracle database connections. They are very sensitive if you are working in the “dedicated” connection mode.
Coming to the above code, I used “OracleConnection” (to connect to the Oracle database) and “OracleCommand” (to specify a table name) classes. Surprisingly, in the above code, you already worked with “OracleDataReader” (but indirectly) without even noticing. The statement “cmd.ExecuteReader” actually returns an instance of “OracleDataReader”, which has been directly assigned to the “datagrid” (which automatically pulls information from “OracleDataReader”).
You need to concentrate a bit on the following statement as well:
cmd.CommandType = CommandType.TableDirect
The above statement specifies to “OracleCommand” that you are not providing any SQL command to process, but simply passing a direct table name to fetch entire information from that table. This would be a bit different in the case of an SQL statement (explained in the next section).
Next: Getting data with the SELECT command >>
More ASP.NET Articles
More By Jagadish Chaterjee