c#windows窗体应用程序连接oracle数据库,怎么实现数据的交互?

2024-11-27 23:51:36
推荐回答(1个)
回答1:

一:通过System.Data.OracleClient(需要安装Oracle客户端并配置tnsnames.ora)
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OracleClient;
3.
string connString = "User ID=IFSAPP;Password=IFSAPP;Data Source=RACE;";
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
二:通过System.Data.OracleClient(需要安装Oracle客户端不需配置tnsnames.ora)
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OracleClient;
3.
string connString = "User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
三:通过System.Data.OleDb和Oracle公司的驱动
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OleDb;
3.
string connString = "Provider=OraOLEDB.Oracle.1;User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
四:通过System.Data.OleDb和微软公司的Oracle驱动
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OleDb;
3.
string connString = "Provider=MSDAORA.1;User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";
OleDbConnection cnn = new OleDbConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}