Oracle Database Interaction Using ODP.NET and ASP.NET: All Ways to Retrieve Data - Getting data with the SELECT command
(Page 4 of 5 )
Let us first go through the example.
Dim cn As New OracleConnection("User
ID=scott;password=tiger;Data Source=ORCL")
Try
Dim cmd As New OracleCommand
cmd.CommandText = "select * from scott.emp"
cmd.CommandType = CommandType.Text
cmd.Connection = cn
cmd.Connection.Open()
Dim rdr As OracleDataReader = cmd.ExecuteReader
Me.DataGrid1.DataSource = rdr
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
Coming to the above code, I used “OracleConnection” (to connect to Oracle database) and “OracleCommand” (to specify a SELECT statement) classes. Here, I added a separate statement (for demonstration purposes only) to work with “OracleDataReader”. You could even proceed, just like the code in the previous section. The statement “cmd.ExecuteReader” actually returns an instance of “OracleDataReader” (now into the reference “rdr”), where “rdr” 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.Text
The above statement specifies to “OracleCommand” that you are not providing table name, but instead an SQL command (it could be any DML command) to process or fetch information from the table. This is quite different from the previous section.
This approach is preferred over the one in the previous section. By using this approach, you can filter (using WHERE clause) or even join (with various types of joins) with other tables and get what is required. This is my favorite method for dealing with SELECT statements (if not using stored procedures or functions).
Next: Getting a single value from the database >>
More ASP.NET Articles
More By Jagadish Chaterjee