频道栏目
首页 > 数据库 > Oracle > 正文
.NET/C龙8国际娱乐pt老虎机/Oracle数据库操作类实例代码
2018-01-16 10:26:33           
收藏   我要投稿

.NET/C龙8国际娱乐pt老虎机/Oracle数据库操作类实例代码

using System;  
using System.Data;  
using System.Collections.Generic;  
using System.Configuration;  
using System.Data.OracleClient;  
using System.Text;  
using System.IO;  
/// <summary>  
/// Oracle数据库操作类  
/// </summary>  
internal static class OracleHelper  
{  
    //数据库连接字符串  
    private readonly static string connstr = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;  
    /// <summary>  
    /// 执行数据库查询操作,返回受影响的行数  
    /// </summary>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作影响的数据行数</returns>  
    internal static int ExecuteNonQuery(string cmdText, params OracleParameter[] commandParameters)  
    {  
        OracleCommand command = new OracleCommand();  
        OracleConnection connection = new OracleConnection(connstr);  
        int result = 0;  
        try  
        {  
            PrepareCommand(command, connection, null,CommandType.Text, cmdText, commandParameters);  
            result = command.ExecuteNonQuery();  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return result;  
    }  
    /// <summary>  
    /// 执行数据库事务查询操作,返回受影响的行数  
    /// </summary>  
    /// <param name="transaction">数据库事务对象</param>  
    /// <param name="cmdType">Command类型</param>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前事务查询操作影响的数据行数</returns>  
    internal static int ExecuteNonQuery(OracleTransaction transaction, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
    {  
        OracleCommand command = new OracleCommand();  
        OracleConnection connection = transaction.Connection;  
        int result = 0;  
        try  
        {  
            PrepareCommand(command, connection, transaction, cmdType, cmdText, commandParameters);  
            result = command.ExecuteNonQuery();  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            transaction.Dispose();  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return result;  
    }  
    /// <summary>  
    /// 执行数据库查询操作,返回受影响的行数  
    /// </summary>  
    /// <param name="connection">Oracle数据库连接对象</param>  
    /// <param name="cmdType">Command类型</param>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作影响的数据行数</returns>  
    internal static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
    {  
        if (connection == null) throw new ArgumentNullException("当前数据库连接不存在");  
        OracleCommand command = new OracleCommand();  
        int result = 0;  
        try  
        {  
            PrepareCommand(command, connection, null, cmdType, cmdText, commandParameters);  
            result = command.ExecuteNonQuery();  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return result;  
    }  
    /// <summary>  
    /// 执行数据库查询操作,返回OracleDataReader类型的内存结果集  
    /// </summary>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作返回的OracleDataReader类型的内存结果集</returns>  
    internal static OracleDataReader ExecuteReader(string cmdText, params OracleParameter[] commandParameters)  
    {  
        OracleCommand command = new OracleCommand();  
        OracleConnection connection = new OracleConnection(connstr);  
        OracleDataReader reader = null;  
        try  
        {  
            PrepareCommand(command, connection, null,CommandType.Text, cmdText, commandParameters);  
            reader = command.ExecuteReader(CommandBehavior.CloseConnection);  
            command.Parameters.Clear();  
            return reader;  
        }  
        catch  
        {  
            command.Dispose();  
            connection.Close();  
            throw;  
        }  
    }  
    /// <summary>  
    /// 执行数据库查询操作,返回DataSet类型的结果集  
    /// </summary>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作返回的DataSet类型的结果集</returns>  
    internal static DataSet ExecuteDataSet(string cmdText, params OracleParameter[] commandParameters)  
    {  
        OracleCommand command = new OracleCommand();  
        OracleConnection connection = new OracleConnection(connstr);  
        DataSet dataset = null;  
        try  
        {  
            PrepareCommand(command, connection, null,CommandType.Text, cmdText, commandParameters);  
            OracleDataAdapter adapter = new OracleDataAdapter();  
            adapter.SelectCommand = command;  
            dataset = new DataSet();  
            adapter.Fill(dataset);  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return dataset;  
    }  
    /// <summary>  
    /// 执行数据库查询操作,返回DataTable类型的结果集  
    /// </summary>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作返回的DataTable类型的结果集</returns>  
    internal static DataTable ExecuteDataTable(string cmdText, params OracleParameter[] commandParameters)  
    {  
        OracleCommand command = new OracleCommand();  
        OracleConnection connection = new OracleConnection(connstr);  
        DataTable table = null;  
        try  
        {  
            PrepareCommand(command, connection, null,CommandType.Text, cmdText, commandParameters);  
            OracleDataAdapter adapter = new OracleDataAdapter();  
            adapter.SelectCommand = command;  
            table = new DataTable();  
            adapter.Fill(table);  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return table;  
    }  
    /// <summary>  
    /// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值  
    /// </summary>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>  
    internal static object ExecuteScalar(string cmdText, params OracleParameter[] commandParameters)  
    {  
        OracleCommand command = new OracleCommand();  
        OracleConnection connection = new OracleConnection(connstr);  
        object result = null;  
        try  
        {  
            PrepareCommand(command, connection, null,CommandType.Text, cmdText, commandParameters);  
            result = command.ExecuteScalar();  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return result;  
    }  
    ///    <summary>  
    ///    执行数据库事务查询操作,返回结果集中位于第一行第一列的Object类型的值  
    ///    </summary>  
    ///    <param name="transaction">一个已存在的数据库事务对象</param>  
    ///    <param name="commandType">命令类型</param>  
    ///    <param name="commandText">Oracle存储过程名称或PL/SQL命令</param>  
    ///    <param name="commandParameters">命令参数集合</param>  
    ///    <returns>当前事务查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>  
    internal static object ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)  
    {  
        if (transaction == null) throw new ArgumentNullException("当前数据库事务不存在");  
        OracleConnection connection = transaction.Connection;  
        if (connection == null) throw new ArgumentException("当前事务所在的数据库连接不存在");  
        OracleCommand command = new OracleCommand();  
        object result = null;  
        try  
        {  
            PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters);  
            result = command.ExecuteScalar();  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            transaction.Dispose();  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return result;  
    }  
    /// <summary>  
    /// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值  
    /// </summary>  
    /// <param name="connection">数据库连接对象</param>  
    /// <param name="cmdType">Command类型</param>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    /// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>  
    internal static object ExecuteScalar(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
    {  
        if (connection == null) throw new ArgumentException("当前数据库连接不存在");  
        OracleCommand command = new OracleCommand();  
        object result = null;  
        try  
        {  
            PrepareCommand(command, connection, null, cmdType, cmdText, commandParameters);  
            result = command.ExecuteScalar();  
            command.Parameters.Clear();  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            command.Dispose();  
            connection.Close();  
            connection.Dispose();  
        }  
        return result;  
    }  
    /// <summary>  
    /// 执行数据库命令前的准备工作  
    /// </summary>  
    /// <param name="command">Command对象</param>  
    /// <param name="connection">数据库连接对象</param>  
    /// <param name="trans">事务对象</param>  
    /// <param name="cmdType">Command类型</param>  
    /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>  
    /// <param name="commandParameters">命令参数集合</param>  
    private static void PrepareCommand(OracleCommand command, OracleConnection connection, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)  
    {  
        if (connection.State != ConnectionState.Open) connection.Open();  
        command.Connection = connection;  
        command.CommandText = cmdText;  
        command.CommandType = cmdType;  
        if (trans != null) command.Transaction = trans;  
        if (commandParameters != null)  
        {  
            foreach (OracleParameter parm in commandParameters)  
                command.Parameters.Add(parm);  
        }  
    }  
    /// <summary>  
    /// 将.NET日期时间类型转化为Oracle兼容的日期时间格式字符串  
    /// </summary>  
    /// <param name="date">.NET日期时间类型对象</param>  
    /// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE(&龙8国际娱乐pt老虎机39;2007-12-1&龙8国际娱乐pt老虎机39;,&龙8国际娱乐pt老虎机39;YYYY-MM-DD&龙8国际娱乐pt老虎机39;))</returns>  
    internal static string GetOracleDateFormat(DateTime date)  
    {  
        return "TO_DATE(&龙8国际娱乐pt老虎机39;" + date.ToString("yyyy-M-dd") + "&龙8国际娱乐pt老虎机39;,&龙8国际娱乐pt老虎机39;YYYY-MM-DD&龙8国际娱乐pt老虎机39;)";  
    }  
    /// <summary>  
    /// 将.NET日期时间类型转化为Oracle兼容的日期格式字符串  
    /// </summary>  
    /// <param name="date">.NET日期时间类型对象</param>  
    /// <param name="format">Oracle日期时间类型格式化限定符</param>  
    /// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE(&龙8国际娱乐pt老虎机39;2007-12-1&龙8国际娱乐pt老虎机39;,&龙8国际娱乐pt老虎机39;YYYY-MM-DD&龙8国际娱乐pt老虎机39;))</returns>  
    internal static string GetOracleDateFormat(DateTime date, string format)  
    {  
        if (format == null || format.Trim() == "") format = "YYYY-MM-DD";  
        return "TO_DATE(&龙8国际娱乐pt老虎机39;" + date.ToString("yyyy-M-dd") + "&龙8国际娱乐pt老虎机39;,&龙8国际娱乐pt老虎机39;" + format + "&龙8国际娱乐pt老虎机39;)";  
    }  
    /// <summary>  
    /// 将指定的关键字处理为模糊查询时的合法参数值  
    /// </summary>  
    /// <param name="source">待处理的查询关键字</param>  
    /// <returns>过滤后的查询关键字</returns>  
    internal static string HandleLikeKey(string source)  
    {  
        if (source == null || source.Trim() == "") return null;  
        source = source.Replace("[", "[]]");  
        source = source.Replace("_", "[_]");  
        source = source.Replace("%", "[%]");  
        return ("%" + source + "%");  
    }  
    /// <summary>  
    /// 将文本内容写入到数据库的CLOB字段中(不可用:报连接被关闭的异常)  
    /// </summary>  
    /// <param name="connectionString">数据库连接字符串</param>  
    /// <param name="table">数据库表名称</param>  
    /// <param name="where">指定的WHERE条件语句</param>  
    /// <param name="clobField">CLOB字段的名称</param>  
    /// <param name="content">要写入的文本内容</param>  
    internal static void WriteCLOB(string table, string where, string clobField, string content)  
    {  
        if (String.IsNullOrEmpty(connstr) || String.IsNullOrEmpty(table) || String.IsNullOrEmpty(clobField)) return;  
        using (OracleConnection connection = new OracleConnection(connstr))  
        {  
            OracleCommand command = null;  
            try  
            {  
                connection.Open();  
                command = connection.CreateCommand();  
                command.CommandText = "SELECT " + clobField + " FROM " + table + " WHERE " + where + " FOR UPDATE";  
                OracleDataReader reader = command.ExecuteReader();  
                if (reader != null && reader.HasRows)  
                {  
                    reader.Read();  
                    command.Transaction = command.Connection.BeginTransaction();  
                    OracleLob lob = reader.GetOracleLob(0);  
                    byte[] buffer = Encoding.Unicode.GetBytes(content);  
                    if (lob != OracleLob.Null) lob.Erase();  
                    lob.Write(buffer, 0, ((buffer.Length % 2 == 0) ? buffer.Length : (buffer.Length - 1)));  
                    command.Transaction.Commit();  
                    reader.Close();  
                }  
            }  
            catch  
            {  
                command.Transaction.Rollback();  
                throw;  
            }  
            finally  
            {  
                command.Dispose();  
                connection.Close();  
                connection.Dispose();  
            }  
        }  
    }  
    /// <summary>  
    /// 从数据库中读取CLOB字段的内容并进行输出  
    /// </summary>  
    /// <param name="connectionString">数据库连接字符串</param>  
    /// <param name="table">数据库表名称</param>  
    /// <param name="where">指定的WHERE条件语句</param>  
    /// <param name="clobField">CLOB字段的名称</param>  
    /// <param name="output">保存内容输出的字符串变量</param>  
    internal static void ReadCLOB(string connectionString, string table, string where, string clobField, ref string output)  
    {  
        if (String.IsNullOrEmpty(connectionString) || String.IsNullOrEmpty(table) || String.IsNullOrEmpty(clobField)) return;  
        using (OracleConnection connection = new OracleConnection(connectionString))  
        {  
            OracleCommand command = null;  
            StreamReader stream = null;  
            try  
            {  
                connection.Open();  
                command = connection.CreateCommand();  
                command.CommandText = "SELECT " + clobField + " FROM " + table + " WHERE " + where;  
                OracleDataReader reader = command.ExecuteReader();  
                if (reader != null && reader.HasRows)  
                {  
                    reader.Read();  
                    command.Transaction = command.Connection.BeginTransaction();  
                    OracleLob lob = reader.GetOracleLob(0);  
                    if (lob != OracleLob.Null)  
                    {  
                        stream = new StreamReader(lob, Encoding.Unicode);  
                        output = stream.ReadToEnd().Trim();  
                        command.Transaction.Commit();  
                        reader.Close();  
                    }  
                }  
            }  
            catch  
            {  
                command.Transaction.Rollback();  
                throw;  
            }  
            finally  
            {  
                stream.Close();  
                command.Dispose();  
                connection.Close();  
                connection.Dispose();  
            }  
        }  
    }  
}  
点击复制链接 与好友分享!回本站首页
上一篇:oracle加密procedure的方法
下一篇:Oracle如何动态显示日志?
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑--致力于做实用的IT技术学习网站

龙8国际娱乐pt老虎机