Вы находитесь на странице: 1из 3

========================================================================

vb.net to oracle connection


===================================

1. add reference
2. oracle.dataaccess.dll
3. imports system.data ' vb.net
imports oracle.dataaccess.client ' odp.net oracle managed provider

using system.data; // c#
using oracle.dataaccess.client; // odp.net oracle managed provider

4. dim oradb as string = "data source=oradb;user id=scott;password=tiger;" '


vb.net

string oradb = "data source=oradb;user id=scott;password=tiger;"; // c#

you can modify the connection string to obviate the need for the tnsnames.ora
file, however. simply replace the name of the alias with how it would be defined
in a tnsnames.ora file.

' vb.net
dim oradb as string = "data source=(description=" _
+ "(address_list=(address=(protocol=tcp)(host=otnsrvr)(port=1521)))" _
+ "(connect_data=(server=dedicated)(service_name=orcl)));" _
+ "user id=scott;password=tiger;"

string oradb = "data source=(description=" // c#


+ "(address_list=(address=(protocol=tcp)(host=otnsrvr)(port=1521)))"
+ "(connect_data=(server=dedicated)(service_name=orcl)));"
+ "user id=scott;password=tiger;";

dim conn as new oracleconnection(oradb) ' vb.net

oracleconnection conn = new oracleconnection(oradb); // c#

notice that the connection string is associated with the connection object by
being passed through the object's constructor, which is overloaded. the
constructor's other overload allows the following alternative syntax:

dim conn as new oracleconnection() ' vb.net


conn.connectionstring = oradb

oracleconnection conn = new oracleconnection(); // c#


conn.connectionstring = oradb;

after associating a connection string with a connection object, use the open
method to make the actual connection.

conn.open() ' vb.net

conn.open(); // c#

we'll cover error handling later.

command object
the command object is used to specify the sql command text that is executed,
either a sql string or a stored procedure. similar to the connection object, it
must be instantiated from its class and it has an overloaded constructor.

dim sql as string = "select dname from dept where deptno = 10" ' vb.net
dim cmd as new oraclecommand(sql, conn)
cmd.commandtype = commandtype.text

string sql = "select dname from dept where deptno = 10"; // c#


oraclecommand cmd = new oraclecommand(sql, conn);
cmd.commandtype = commandtype.text;

using different overloads, the syntax can be structured slightly differently. the
command object has methods for executing the command text. different methods are
appropriate for different types of sql commands.

retrieving a scalar value

retrieving data from the database can be accomplished by instantiating a


datareader object and using the executereader method, which returns an
oracledatareader object. vb.net developers can access returned data by passing
either the column name or zero-based column ordinal to the item property. another
option is to use accessor type methods to return column data.

dim dr as oracledatareader = cmd.executereader() ' vb.net


dr.read()
label1.text = dr.item("dname") ' retrieve by column name
label1.text = dr.item(0) ' retrieve the first column in the select list
label1.text = dr.getstring(0) ' retrieve the first column in the select list

c# developers must use accessor type methods for retrieving data. there are typed
accessors for returning .net native data types and others for returning native
oracle data types. zero-based ordinals are passed to the accessors to specify
which column to return.

oracledatareader dr = cmd.executereader(); // c#
dr.read();
label1.text = dr.getstring(0); // c# retrieve the first column in the select list

===============================================
error handling

try-catch-finally structured error handling is a part of .net languages. here is a


relatively minimalist example of using the try-catch-finally syntax:

dim conn as new oracleconnection(oradb) ' vb.net


try
conn.open()

dim cmd as new oraclecommand


cmd.connection = conn
cmd.commandtext = "select dname from dept where deptno = " + textbox1.text
cmd.commandtype = commandtype.text

if dr.read() then
label1.text = dr.item("dname") ' or use dr.item(0)
end if
catch ex as exception ' catches any error
messagebox.show(ex.message.tostring())
finally
conn.dispose()
end try

oracleconnection conn = new oracleconnection(oradb); // c#


try
{
conn.open();

oraclecommand cmd = new oraclecommand();


cmd.connection = conn;
cmd.commandtext = "select dname from dept where deptno = " + textbox1.text;
cmd.commandtype = commandtype.text;

if (dr.read()) // c#
{
label1.text = dr.getstring(0);
}
}
catch (exception ex) // catches any error
{
messagebox.show(ex.message.tostring());
}
finally
{
conn.dispose();
}

Вам также может понравиться