Академический Документы
Профессиональный Документы
Культура Документы
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
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;"
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:
after associating a connection string with a connection object, use the open
method to make the actual connection.
conn.open(); // c#
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
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.
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
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
if (dr.read()) // c#
{
label1.text = dr.getstring(0);
}
}
catch (exception ex) // catches any error
{
messagebox.show(ex.message.tostring());
}
finally
{
conn.dispose();
}