随着时间的推移知识也在更新,原来可用的技术也会被淘汰或更新。

framework4.0开始不再支持System.Data.OracleClient了,但是令人欣慰的是ORACLE公司自己出了一个Oracle.ManagedDataAccess链接库。

下载地址:http://files.cnblogs.com/files/weipt/OracleBase.rar

添加引用之后就可以在c#中使用了。

1.连接字符串如下

<connectionStrings>
<add name="ConnectionString" connectionString="server=server;uid=sa;pwd=sa;database=dy_db;"/>
<add name="ConnectionString_Oralce" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.3.254)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=tdc)));Persist Security Info=True;User ID=tdc;Password=tdc;"/>
</connectionStrings>

不用建立tns监听文件,不用安装庞大的oracle数据库客户端

2.建立公共的数据访问方法

    /// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString,connection))
{
try
{
connection.Open();
int rows=cmd.ExecuteNonQuery();
return rows;
}
catch(OracleException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
        /// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString,params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand())
{
try
{
PrepareCommand(cmd, connection, null,SQLString, cmdParms);
int rows=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch(OracleException E)
{
throw new Exception(E.Message);
}
}
}
}
     /// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString,connection);
command.Fill(ds,"ds");
}
catch(OracleException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}

3.使用时和sqlserver不同的地方

1>如果采用sql字符串查询,那么查询语句要遵循ORACLE标准,日期格式要用

to_date('2017-05-24 12:12:12','yyyy-mm-dd hh24:mi:ss')

2>不支持top关键字等关键字

3>如果用传参执行的化,不用转换日期格式,但是不是@了,而是:冒号

     /// <summary>
/// 更新一条数据
/// </summary>
public bool Update(OMaticsoft.Model.MDEVICECRUNTIME model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update DEVICECRUNTIME set ");
strSql.Append("PARAVALUE=:PARAVALUE,");
strSql.Append("RECEIVETIME=:RECEIVETIME");
strSql.Append(" where DEVICEID=:DEVICEID and RESNAME=:RESNAME");
OracleParameter[] parameters = {
new OracleParameter("PARAVALUE", OracleDbType.NVarchar2,),
new OracleParameter("RECEIVETIME", OracleDbType.Date),
new OracleParameter("DEVICEID", OracleDbType.Int32,),
//new OracleParameter("RUMTIMEID", OracleDbType.Long,4),
new OracleParameter("RESNAME", OracleDbType.NVarchar2,)};
parameters[].Value = model.PARAVALUE;
parameters[].Value = model.RECEIVETIME;
parameters[].Value = model.DEVICEID;
//parameters[3].Value = model.RUMTIMEID;
parameters[].Value = model.RESNAME; int rows=DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
if (rows > )
{
return true;
}
else
{
return false;
}
}