C uses ODP.net to connect to the Oracle database
- 2020-12-07 04:33:22
- OfStack
This article describes the process of connecting C# to the Oracle database. With instant client and ODP.DataAccess.dll in net, you can easily deploy.net applications or sites without having to install the Oracle client. So let's talk about the 1 process.
1. Installation of ODAC
Download the version of ODAC that corresponds to the version of oracle you installed from oracle's official website.
Download address: ODAC Download
After downloading and unzip the installation, you don't need to install all the components. The following components are mainly installed:
Oracle Instant Client
Oracle Data Provider For .net2.0
Oracle rovider For Asp .net
2. Setting environment variables
Set the environment variable of Windows:
ORACLE_HOME: Installation directory for ODAC (similar to ~ \app\Administrator\product\11.1.0\client_1);
ORACLE_HOME LD_LIBRARY_PATH: % %;
ORACLE_HOME TNS_ADMIN: % %;
Append to the front of PATH: %ORACLE_HOME%;
3. Listen to the configuration file tnsnames.ora
In the directory %ORACLE_HOME%, create a new file tnsnames.ora, which reads as follows:
4. plsqldev
With this configuration, plsqldev can connect to the oracle database.
5. C# connect Oracle
The sample code for C# to connect oracle is as follows:
Appends the configuration of the database connection to the program ES81en.config or ES83en.config.
Follow these steps and, if there are no errors, you can successfully connect to the database.
1. Installation of ODAC
Download the version of ODAC that corresponds to the version of oracle you installed from oracle's official website.
Download address: ODAC Download
After downloading and unzip the installation, you don't need to install all the components. The following components are mainly installed:
Oracle Instant Client
Oracle Data Provider For .net2.0
Oracle rovider For Asp .net
2. Setting environment variables
Set the environment variable of Windows:
ORACLE_HOME: Installation directory for ODAC (similar to ~ \app\Administrator\product\11.1.0\client_1);
ORACLE_HOME LD_LIBRARY_PATH: % %;
ORACLE_HOME TNS_ADMIN: % %;
Append to the front of PATH: %ORACLE_HOME%;
3. Listen to the configuration file tnsnames.ora
In the directory %ORACLE_HOME%, create a new file tnsnames.ora, which reads as follows:
The database SID =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = Oracle Host name or IP)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = The database SID)
)
)
4. plsqldev
With this configuration, plsqldev can connect to the oracle database.
5. C# connect Oracle
The sample code for C# to connect oracle is as follows:
OracleConnection conn =
new OracleConnection();
try
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["oradb"].ConnectionString;
conn.Open();
string sql = " select id,content from test"; // C#
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader(); // C#
List<string> contents = newList<string>();
while(dr.Read())
{
contents.Add(dr["content"].ToString());
}
listBox1.ItemsSource = contents;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Clone();
}
Appends the configuration of the database connection to the program ES81en.config or ES83en.config.
<connectionStrings >
<add name="oradb"connectionString="Data Source=(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=****)));
User Id=***;Password=***;"/>
</connectionStrings>
Follow these steps and, if there are no errors, you can successfully connect to the database.