1.ODAC 下载方法和安装和教程
在window 应用程序中访问,访问oracle就可以不用安装oracle client了,直接设定tns name 可访问数据库。同时,我又编定了一个操作oracel数据库帮助类,供大家参考
1.1. 下载路径
1.1.1 32位下载路径
下载网址:https://www.oracle.com/database/technologies/odac-nuget-downloads.html
1.1.2 64位下载路径
下载网址:https://www.oracle.com/database/technologies/odac-downloads.html
1.2 安装教程
1.2.1 :直接查看压缩包中 readme.htm安装方法
1.2.1. 或者按如下网络上的教程进行安装
https://www.cnblogs.com/wayne-ivan/p/4691091.html
1.2.3 Oracle官网查询相关ODC的资料,列出相关资源和文件
注:1. 安装时必须有管理员权限;2.在安装过程中如有什么错误,请安装路径下查看log档,进行异常处理。
2. 在应用程序中引用
2.1 配件文件中使用如下所示 Provider=OraOLEDB.Oracle 的驱动来读取数据库
<add key="DatabaseP1" value="Provider=OraOLEDB.Oracle;Data Source=DB1;User ID=DBA;Password=DBA;Persist Security Info=True"/>
2.2 程序中引用命名空间 using Oracle.DataAccess;
2.3 访问数据实例
可发根据设定Provider来设定相应数据库的驱动,可以实现连接MY SQL ,MS SQL,ORACEL等数据库操作
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();
}
2.4 操作数据库帮助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
/// <summary>
/// SqlDbHelper ,以System.Data.OleDb類來操作數據庫
/// </summary>
public class OleDbHelper
{
public string strMsg = "";
public OleDbConnection conn;
/// <summary>
/// SqlDbHelper ,以System.Data.OleDb類來操作數據庫
/// </summary>
public OleDbHelper()
{
//
// TODO: 在此加入建構函式的程式碼
//
//this.GetConn();
}
public OleDbHelper(String sDbConnString)
{
this.GetConn(sDbConnString);
}
/// <summary>
/// 是否打开数据库连接
/// </summary>
/// <param name="IsOpenConn"></param>
public OleDbHelper(Boolean IsOpenConn)
{
//
// TODO: 在此加入建構函式的程式碼
//
if (IsOpenConn)
{
this.GetConn();
}
}
public string Message
{
get
{
return strMsg;
}
set
{
strMsg = value;
}
}
public OleDbConnection Connection
{
set { conn = value; }
get { return conn; }
}
/// <summary>
/// Get DB conncection string from web.config file.
/// By OleDbConnString
/// </summary>
/// <returns></returns>
public string GetConnectString()
{
string strConn = "";
try
{
//Get DB conncection string from web.config file.
strConn = "";
}
catch (Exception ex)
{
this.Message = ex.Message;
}
return strConn;
}
/// <summary>
/// 取得Sqlcle数据库存连接,需在web.config中的appSetting处设定连接字符串
/// MsAccessDbConnString的DB连接字符串
/// </summary>
/// <returns></returns>
public OleDbConnection GetConn()
{
string strConn;
string strDataSource;
string strDataFile;
OleDbConnection Conn = new OleDbConnection();
try
{
//Get DB conncection string from web.config file.
strDataFile =(System.Environment.CurrentDirectory+@"/db/data.mdb");
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDataFile;
Conn.ConnectionString = strConn;
Conn.Open();
//增加属性
this.Connection = Conn;
return Conn;
}
catch (Exception ex)
{
this.Message = ex.Message + ex.StackTrace + ex.Source;
return null;
}
}
/// <summary>
/// 取得Access数据库存连接,
/// 需在web.config中的appSetting处设定连接字符串sSqlDbConnString Key的值
/// </summary>
/// <param name="sSqlDbConnString">在web.config中的appSetting处设定连接字符串名稱</param>
/// <returns></returns>
public OleDbConnection GetConn(string sSqlDbConnString)
{
string strConn;
string strDataFile = "";
string strDataSource = "";
OleDbConnection Conn = new OleDbConnection();
try
{
//Get DB conncection string from web.config file.
Conn.ConnectionString = sSqlDbConnString;
Conn.Open();
return Conn;
}
catch (Exception ex)
{
this.Message = ex.Message + ex.Source + ex.StackTrace;
return null;
}
}
/// <summary>
/// 取得Sqlcle数据库存连接
/// </summary>
/// <param name="sTNSName">连接SqlCLE的tnsname</param>
/// <param name="sUserId">用户名</param>
/// <param name="sPassword">密码</param>
/// <returns></returns>
public OleDbConnection GetConn(string sServerName, string sDataBaseName, string sUserId, string sPassword)
{
string strConn;
try
{
OleDbConnection Conn = new OleDbConnection();
strConn = "Provider=SQLOLEDB;server=" + sServerName + ";database=" + sDataBaseName + ";uid=" + sUserId + ";pwd=" + sPassword;
Conn.ConnectionString = strConn;
Conn.Open();
return Conn;
}
catch
{
return null;
}
}
/// <summary>
/// 打开数据库,在WebConfig中对应的appSettings Key值
/// </summary>
/// <param name="sSqlDbConnString"></param>
/// <returns></returns>
public Boolean OpenConnKey(string sSqlDbConnString)
{
string strConn;
try
{
OleDbConnection Conn = new OleDbConnection();
//Get DB conncection string from web.config file.
strConn = "";
Conn.ConnectionString = strConn;
Conn.Open();
this.Connection = Conn;
return true;
}
catch (Exception ex)
{
this.Message = ex.Message;
return false;
}
}
/// <summary>
/// 打开连接字符串
/// </summary>
/// <param name="sSqlDbConnString"></param>
/// <returns></returns>
public Boolean OpenConnString(string sDbConnString)
{
OleDbConnection Conn = new OleDbConnection();
try
{
Conn.ConnectionString = sDbConnString;
Conn.Open();
this.Connection = Conn;
return true;
}
catch (Exception ex)
{
this.Message = ex.Message;
return false;
}
}
/// <summary>
/// 取得数据集
/// </summary>
/// <param name="strSQL"></param>
/// <param name="Conn"></param>
/// <returns></returns>
public DataSet GetDataSet(string strSQL, OleDbConnection Conn)
{
OleDbDataAdapter Adpter;
DataSet ds;
try
{
//strSQL = "select * from t_PublicInfo ";
Adpter = new OleDbDataAdapter(strSQL, Conn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
}
catch
{
return null;
}
return ds;
}
/// <summary>
/// 得到数据集,Webconfig add设定的的Key值
/// </summary>
/// <param name="strSQL"></param>
/// <param name="sConnString">Webconfig add设定的的Key值</param>
/// <returns></returns>
public DataSet GetDataSet(string strSQL, string sConnString)
{
OleDbDataAdapter Adpter;
DataSet ds;
OleDbConnection conn = new OleDbConnection();
try
{
conn = this.GetConn(sConnString);
//strSQL = "select * from t_PublicInfo ";
Adpter = new OleDbDataAdapter(strSQL, conn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
}
catch (Exception ex)
{
this.Message = ex.Message;
return null;
}
finally
{
conn.Close();
conn.Dispose();
}
return ds;
}
/// <summary>
/// 取得数据集,默認連接的數據庫存是在web.config中的appSetting处设定连接字符串SqlDbConnString
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public DataSet GetDataSet(string strSQL)
{
//string strSQL;
OleDbDataAdapter Adpter;
DataSet ds;
OleDbConnection Conn = new OleDbConnection();
Conn = GetConn();
if (Conn == null)
{
return null;
}
try
{
Adpter = new OleDbDataAdapter(strSQL, Conn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
}
catch (Exception ex)
{
this.Message = ex.Message;
return null;
}
finally
{
conn.Close();
}
return ds;
}
/// <summary>
/// 取得DataReader,默認連接的數據庫存是在web.config中的appSetting处设定连接字符串SqlDbConnString
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public OleDbDataReader GetReader(string strSQL)
{
//OleDbDataAdapter Adpter;
OleDbDataReader oReader;
OleDbConnection Conn = new OleDbConnection();
OleDbCommand oComm = new OleDbCommand();
Conn = GetConn();
oComm.Connection = Conn;
oComm.CommandText = strSQL;
try
{
oReader = oComm.ExecuteReader();
}
catch
{
return null;
}
return oReader;
}
/// <summary>
/// 取得DataReader
/// </summary>
/// <param name="strSQL"></param>
/// <param name="oConn"></param>
/// <returns></returns>
public OleDbDataReader GetReader(string strSQL, OleDbConnection oConn)
{
//OleDbDataAdapter Adpter;
OleDbDataReader oReader;
OleDbCommand oComm = new OleDbCommand();
oComm.Connection = oConn;
oComm.CommandText = strSQL;
try
{
oReader = oComm.ExecuteReader();
}
catch
{
return null;
}
return oReader;
}
/// <summary>
/// 得到一张表,根据SQL语句,如果沒有找到資料,連接正確,行個數為0,如果連接異常的,返回為空
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public DataTable GetDataTable(string strSql)
{
OleDbDataAdapter Adpter;
DataSet ds;
DataTable dtTable = new DataTable();
OleDbConnection Conn = new OleDbConnection();
Conn = this.GetConn();
if (Conn == null)
{
return dtTable;
}
try
{
Adpter = new OleDbDataAdapter(strSql, Conn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
dtTable = ds.Tables[0];
this.Message = "";
}
catch (Exception ex)
{
this.Message = ex.Message + ex.StackTrace;
}
finally
{
conn.Close();
}
return dtTable;
}
public DataTable GetDataTable(string strSql, string sConnStringKey)
{
OleDbDataAdapter Adpter;
DataSet ds;
DataTable dtTable = new DataTable();
OleDbConnection myConn = new OleDbConnection();
myConn = this.GetConn(sConnStringKey);
if (myConn == null)
{
return dtTable;
}
try
{
Adpter = new OleDbDataAdapter(strSql, myConn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
dtTable = ds.Tables[0];
this.Message = "";
}
catch (Exception ex)
{
this.Message = ex.Message + ex.StackTrace;
}
finally
{
myConn.Close();
}
return dtTable;
}
/// <summary>
/// 执行 sql command
/// </summary>
/// <param name="strSQL"></param>
/// <param name="Conn"></param>
/// <returns></returns>
public string ExecuteNonQuery(string strSQL, OleDbConnection Conn)
{
OleDbCommand Cmd = new OleDbCommand();
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.Connection = Conn;
Cmd.Transaction = Conn.BeginTransaction();
try
{
Cmd.ExecuteNonQuery();
Cmd.Transaction.Commit();
return "";
}
catch (Exception Err)
{
Cmd.Transaction.Rollback();
return Err.Message;
}
finally
{
Cmd.Dispose();
}
}
/// <summary>
/// 执行 sql command,默認連接的數據庫存是在web.config中的appSetting处设定连接字符串SqlDbConnString
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public Boolean ExecuteNonQuery(string strSQL)
{
OleDbConnection Conn = new OleDbConnection();
Conn = GetConn();
OleDbCommand Cmd = new OleDbCommand();
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = strSQL;
Cmd.Connection = Conn;
Cmd.Transaction = Conn.BeginTransaction();
try
{
Cmd.ExecuteNonQuery();
Cmd.Transaction.Commit();
return true;
}
catch (Exception Err)
{
Cmd.Transaction.Rollback();
this.Message = Err.Message + ";" + strSQL;
return false;
}
finally
{
Cmd.Dispose();
conn.Close();
}
}
public Boolean ExecuteNonQuery(string strSQL, string sConnStringKey)
{
OleDbConnection myConn = new OleDbConnection();
myConn = GetConn(sConnStringKey);
OleDbCommand myCommand = new OleDbCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = strSQL;
myCommand.Connection = myConn;
myCommand.Transaction = myConn.BeginTransaction();
try
{
myCommand.ExecuteNonQuery();
myCommand.Transaction.Commit();
return true;
}
catch (Exception Err)
{
myCommand.Transaction.Rollback();
this.Message = Err.Message + ";" + strSQL;
return false;
}
finally
{
myCommand.Dispose();
myConn.Close();
}
}
/// <summary>
/// 得到一张表,根据SQL语句,如果沒有找到資料,連接正確,行個數為0,如果連接異常的,返回為空
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public Boolean GetExistsData(string strSql)
{
Boolean bResult = false;
OleDbDataAdapter Adpter;
DataSet ds;
DataTable dtTable = new DataTable();
OleDbConnection Conn = new OleDbConnection();
Conn = this.GetConn();
if (Conn == null)
{
return false;
}
try
{
Adpter = new OleDbDataAdapter(strSql, Conn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
dtTable = ds.Tables[0];
if (dtTable.Rows.Count > 0)
{
bResult = true;
}
}
catch (Exception ex)
{
this.Message = ex.Message + ex.StackTrace;
bResult = false;
}
return bResult;
}
public Boolean GetExistsData(string strSql, string sConnStringKey)
{
Boolean bResult = false;
OleDbDataAdapter Adpter;
DataSet ds;
DataTable dtTable = new DataTable();
OleDbConnection Conn = new OleDbConnection();
//Conn = this.GetConn();
Conn = this.GetConn(sConnStringKey);
if (Conn == null)
{
return false;
}
try
{
Adpter = new OleDbDataAdapter(strSql, Conn);
ds = new DataSet();
Adpter.Fill(ds, "table1");
dtTable = ds.Tables[0];
if (dtTable.Rows.Count > 0)
{
bResult = true;
}
}
catch (Exception ex)
{
this.Message = ex.Message + ex.StackTrace;
bResult = false;
}
return bResult;
}
public void Close()
{
if (this.Connection.State == ConnectionState.Open)
{
this.Connection.Close();
}
}
}
今天的文章oracle 访问工具_查看已安装的oracle组件分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/83971.html