1.首先去下载Oracle驱动oracle_net.msi
2.安装完之后在项目里面引用system.data.oracleclient
3.链接数据库的GetConnection类如下:
public class GetConnection
{
private static OracleConnection _connection;
///
/// 获取数据库连接桥
///
private static OracleConnection Connection
{
get
{
//Data Source=orcl_169;User ID=openeap;Password=***********;Unicode=True
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
// string connectionString = "Data Source=.;Initial Catalog=shopping;Persist Security Info=True;User ID=dkj;Password=dkj123456";
//string connectionString = "Data Source=.;Initial Catalog=shopping;Integrated Security=True";;dkj/dkj123456
if (_connection == null)
{
_connection = new OracleConnection(connectionString);
_connection.Open();
}
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
if (_connection.State == ConnectionState.Broken)
{
_connection.Close();
_connection.Open();
}
return GetConnection._connection;
}
}
///
/// 获取表数据
///
///
///
public static OracleDataReader GetDataRead(string sql)
{
OracleCommand command = new OracleCommand(sql, Connection);
OracleDataReader read = command.ExecuteReader();
return read;
}
public static int NoSelect(string sql)
{
OracleCommand command = new OracleCommand(sql, Connection);
int row = command.ExecuteNonQuery();
return row;
}
public static DataTable GetDataTable(string sql)
{
OracleCommand command = new OracleCommand(sql, Connection);
DataTable dt = new DataTable();
OracleDataAdapter sda = new OracleDataAdapter(command);
sda.Fill(dt);
return dt;
}
///
/// 执行sql语句,返回一行一列。。
///
/// SQL语句
///
public static string GetScalar(string sql)
{
OracleCommand command = new OracleCommand(sql, Connection);
return command.ExecuteScalar().ToString();
}
}