首页 新闻 搜索 专区 学院

谁有.NET连接ORACLE的程序

0
悬赏园豆:200 [待解决问题]

谁有.NET连接ORACLE的程序

问题补充: 或者找人写一个,谁会的留下联系
heise的主页 heise | 初学一级 | 园豆:0
提问于:2011-03-15 18:10
< >
分享
所有回答(7)
0

直接通过VS2005新建数据窗体向导就可以获取到连接ORACLE的后台代码了。

深水的猫 | 园豆:110 (初学一级) | 2011-03-15 20:45
0

先在vs2005或是2008中指定是哪种数据库连接类型,然后使用它的时候再写一些代码就行了

_OleDbConnection = new OleDbConnection(connectionString); 
      _OleDbConnection.Open();这是接连Oracle的代码,也可以通过其它类型,比如说,String类型等等

一直在跑wu | 园豆:150 (初学一级) | 2011-03-15 22:06
0

.net连接oracle的程序?你是要那样的源码?

顾晓北 | 园豆:10219 (专家六级) | 2011-03-16 10:42
0

有一个Oracle.Access.dll

clound | 园豆:481 (菜鸟二级) | 2011-03-16 13:08
0

web.config

<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=dev;User ID=username ; Password=password; Omit Oracle Connection Name=True; enlist=false" providerName="System.Data.OracleClient"/>
</connectionStrings>

接口

using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Utility
{
///<summary>
/// --------------------------------------------------------------
/// Description: 数据据基本操作接口
/// --------------------------------------------------------------
///</summary>
///<typeparam name="T">实体对象</typeparam>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
publicinterface IBaseOperate<T>
{
///<summary>
/// 添加一条记录到表格中
///</summary>
///<param name="model">实体对象</param>
///<returns>返回数据库受影响的记录数</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
int Insert(T model );

///<summary>
/// 更新表格中的一条记录
///</summary>
///<param name="model">实体对象</param>
///<returns>返回数据库受影响的记录数</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
int Update(T model);

///<summary>
/// 删除表格中的一条记录
///</summary>
///<param name="model">实体对象</param>
///<returns>返回数据库受影响的记录数</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
int Delete(T model);

///<summary>
/// 根据主键获取一条记录
///</summary>
///<param name="id">表主键记录ID</param>
///<returns>实体对象</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
T GetModel(string id);

///<summary>
/// 获取此表中所有记录
///</summary>
///<returns>实体对象记录List对象</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
IList<T> GetList();

///<summary>
/// 某条记录是否存在
///</summary>
///<param name="id">表主键记录ID</param>
///<returns>true 存在此ID,false不存在此ID</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
bool Exists(string id);
}
}

 

操作类

using System;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Collections;

namespace Dtsc.Common.Utility
{

///<summary>
/// --------------------------------------------------------------
/// Description: Oracle数据库访问类
/// --------------------------------------------------------------
///</summary>
publicabstractclass OracleHelper
{
///<summary>
/// 数据库连接字符串
///</summary>
publicstaticstring ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//public static readonly string ConnectionString = "Data Source=dtscsmis;User ID=dtscsmis; Password=dtscsmis";

///<summary>
///
///</summary>
privatestatic Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

#region ExecuteNonQuery

///<summary>
/// Executes the non query.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();

using (OracleConnection connection =new OracleConnection(ConnectionString))
{
PrepareCommand(cmd, connection,
null, cmdType, cmdText, commandParameters);

int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}

///<summary>
/// Executes the non query.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteNonQuery(CommandType.Text, cmdText, commandParameters);
}

///<summary>
/// Executes the non query.
///</summary>
///<param name="trans">The trans.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}

///<summary>
/// Executes the non query.
///</summary>
///<param name="connection">The connection.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{

OracleCommand cmd
=new OracleCommand();

PrepareCommand(cmd, connection,
null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
#endregion

#region ExecuteReader

///<summary>
/// Executes the reader.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic OracleDataReader ExecuteReader(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();
OracleConnection conn
=new OracleConnection(ConnectionString);

try
{
PrepareCommand(cmd, conn,
null, cmdType, cmdText, commandParameters);

OracleDataReader rdr
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;

}
catch(Exception exc)
{
conn.Close();
throw exc;
}
}

///<summary>
/// Executes the reader.
///</summary>
///<param name="conn">数据库连接</param>
///<param name="cmdType">命令类型</param>
///<param name="cmdText">命令内容</param>
///<param name="commandParameters">命令参数</param>
///<returns>reader</returns>
publicstatic OracleDataReader ExecuteReader(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();

try
{
PrepareCommand(cmd, conn,
null, cmdType, cmdText, commandParameters);

OracleDataReader rdr
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception exc)
{
conn.Close();
throw exc;
}
}

///<summary>
/// 执行数据读取器(当DataReader关闭时,相关的数据库连接不关闭)
///</summary>
///<param name="conn">数据库连接</param>
///<param name="cmdType">命令类型</param>
///<param name="cmdText">命令内容</param>
///<param name="commandParameters">命令参数</param>
///<returns>reader</returns>
publicstatic OracleDataReader ExecuteReaderWithoutClosingConnection(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();

try
{
PrepareCommand(cmd, conn,
null, cmdType, cmdText, commandParameters);

OracleDataReader rdr
= cmd.ExecuteReader();
cmd.Parameters.Clear();
return rdr;
}
catch (Exception exc)
{
conn.Close();
throw exc;
}
}

///<summary>
/// Executes the reader.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic OracleDataReader ExecuteReader(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteReader(CommandType.Text, cmdText, commandParameters);
}
#endregion

///<summary>
/// Executes the data set.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic DataSet ExecuteDataSet(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteDataSet(CommandType.Text, cmdText, commandParameters);
}

///<summary>
/// Executes the data set.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();

using (OracleConnection conn =new OracleConnection(ConnectionString))
{
PrepareCommand(cmd, conn,
null, cmdType, cmdText, commandParameters);
OracleDataAdapter oda
=new OracleDataAdapter(cmd);
DataSet ds
=new DataSet();
oda.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
}

#region ExecuteScalar

///<summary>
/// Executes the scalar.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();

using (OracleConnection conn =new OracleConnection(ConnectionString))
{
PrepareCommand(cmd, conn,
null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}

///<summary>
/// Executes the scalar.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteScalar(CommandType.Text, cmdText, commandParameters);
}

///<summary>
/// Executes the scalar.
///</summary>
///<param name="transaction">The transaction.</param>
///<param name="commandType">Type of the command.</param>
///<param name="commandText">The command text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
{
if (transaction ==null)
thrownew ArgumentNullException("transaction");
if (transaction !=null&& transaction.Connection ==null)
thrownew ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");

// Create a command and prepare it for execution
OracleCommand cmd =new OracleCommand();

PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);

// Execute the command & return the results
object retval = cmd.ExecuteScalar();

// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
}

///<summary>
/// Executes the scalar.
///</summary>
///<param name="connectionString">The connection string.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(OracleConnection connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd
=new OracleCommand();

PrepareCommand(cmd, connectionString,
null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}

#endregion

///<summary>
/// Caches the parameters.
///</summary>
///<param name="cacheKey">The cache key.</param>
///<param name="commandParameters">The command parameters.</param>
publicstaticvoid CacheParameters(string cacheKey, params OracleParameter[] commandParameters)
{
parmCache[cacheKey]
= commandParameters;
}

///<summary>
/// Gets the cached parameters.
///</summary>
///<param name="cacheKey">The cache key.</param>
///<returns></returns>
publicstatic OracleParameter[] GetCachedParameters(string cacheKey)
{
OracleParameter[] cachedParms
= (OracleParameter[])parmCache[cacheKey];

if (cachedParms ==null)
returnnull;

// If the parameters are in the cache
OracleParameter[] clonedParms =new OracleParameter[cachedParms.Length];

// return a copy of the parameters
for (int i =0, j = cachedParms.Length; i < j; i++)
clonedParms[i]
= (OracleParameter)((ICloneable)cachedParms[i]).Clone();

return clonedParms;
}

///<summary>
/// Prepares the command.
///</summary>
///<param name="cmd">The CMD.</param>
///<param name="conn">The conn.</param>
///<param name="trans">The trans.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
privatestaticvoid PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
{

//Open the connection if required
if (conn.State != ConnectionState.Open)
conn.Open();

//Set up the command
cmd.Connection = conn;
cmd.CommandText
= cmdText;
cmd.CommandType
= cmdType;

//Bind it to the transaction if it exists
if (trans !=null)
cmd.Transaction
= trans;

// Bind the parameters passed in
if (commandParameters !=null)
{
foreach (OracleParameter parm in commandParameters)
cmd.Parameters.Add(parm);
}
}

///<summary>
/// Oras the bit.
///</summary>
///<param name="value">if set to <c>true</c> [value].</param>
///<returns></returns>
publicstaticstring OraBit(bool value)
{
if (value)
return"Y";
else
return"N";
}

///<summary>
/// Oras the bool.
///</summary>
///<param name="value">The value.</param>
///<returns></returns>
publicstaticbool OraBool(string value)
{
if (value.Equals("Y"))
returntrue;
else
returnfalse;
}

///<summary>
/// Safes the value.
///</summary>
///<param name="obj">The obj.</param>
///<returns></returns>
publicstaticobject SafeValue(object obj)
{
if (obj ==null)
return DBNull.Value;
else
return obj;
}

#region IDataBase 成员

///<summary>
/// 获得数据库连接
///</summary>
///<returns>数据库连接</returns>
publicstatic System.Data.Common.DbConnection GetConnection()
{
returnnew OracleConnection(ConnectionString);
}

#endregion
}
}
青 蛙 | 园豆:655 (小虾三级) | 2011-03-16 14:13
0

兄弟,连接方法全给你:

NET连接Oracle的方法 

方式1:直接利用.NET的oracle驱动连接 
引用System.data.oracleclient; 
1 using System.data.oracleclient; 
2 string oradb = "Data Source=water;User Id=modis;Password=modis;Integrated Security=no"; 
3 OracleConnection conn = new OracleConnection(oradb); 
4 conn.Open(); 
(我的环境为VS2005,oracle数据源连接只支持到10g以下。) 
方式2:安装完ODP.net(Oracle Data Provider.NET),通过OLEDB连接。 
引用System.Data.OleDb; 
1 using System.Data.OleDb; 
2 string oradb = "Provider=OraOLEDB.Oracle;Data Source=water;User Id=modis;Password=modis;"; 
3 OleDbConnection myConnection = new OleDbConnection(oradb); 
4 myConnection.Open(); 
方式3:安装完ODP.net,通过Oracle的连接器 
引用动态链接库Oracle.DataAccess.Client. 
1 using Oracle.DataAccess.Client; 
2 string oradb = "Data Source=water;User Id=modis;Password=modis;"; 
3 OracleConnection conn = new OracleConnection(oradb); // C# 
4 conn.Open();

.NET快速开发框架 | 园豆:914 (小虾三级) | 2011-03-17 17:21
0

不要把简单的问题复杂化。

1.装oracle客户端

流浪2011 | 园豆:3 (初学一级) | 2011-04-28 14:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册