DbHelpOracle 操作Oracle的几个方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
//程序集 Microsoft.Practices.EnterpriseLibrary.Data.dll, v5.0.414.0
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using System.Data.Common;
using System.Data;
//程序集 System.Data.OracleClient.dll, v4.0.0.0
using System.Data.OracleClient;

namespace DbHelpOracle
{
    public class DbHelpOracle
    {
        public static string oracleConnectstring = ConfigurationManager.AppSettings["oracleConnectstring"];
        /// 
        /// 执行查询数据的SQL语句
        /// 
        /// 
        /// 
        /// 
        public static DataTable ExcuteQuery(string strSql, params OracleParameter[] commandParameters)
        {
            DataSet ds = new DataSet();
            OracleDatabase dataBase = new OracleDatabase(oracleConnectstring);
            using (DbCommand cmd = PrepareCommand(dataBase, null, CommandType.Text, strSql, commandParameters))
            {
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
                dataBase.LoadDataSet(cmd, ds, dt.TableName);
                cmd.Parameters.Clear();
            }
            return ds.Tables[0];
        }
        /// 
        /// 执行 update delete insert 语句的的方法
        /// 
        /// 
        /// 
        /// 
        public static int ExcuteNoQuery(string strSql, params OracleParameter[] commandParameters)
        {
            //创建数据库
            OracleDatabase database = new OracleDatabase(oracleConnectstring);
            //声明执行对象
            int val = 0;
            using (DbCommand cmd = PrepareCommand(database, null, CommandType.Text, strSql, commandParameters))
            {
                //执行命令
                val = database.ExecuteNonQuery(cmd);
                cmd.Parameters.Clear();
            }
            //返回结果
            return val;
        }
        public static DbCommand PrepareCommand(OracleDatabase con, DbTransaction trans, CommandType cmdType, string strSql, OracleParameter[] commandParameters)
        {
            DbCommand cmd = con.GetSqlStringCommand(strSql);
            cmd.CommandType = cmdType;
            if (trans != null)
                cmd.Transaction = trans;
            if (commandParameters != null)
            {
                foreach (OracleParameter param in commandParameters)
                {
                    if (param.Value == null)
                        param.Value = DBNull.Value;
                    cmd.Parameters.Add(param.Value);
                }
            }
            return cmd;
        }
    }
}

你可能感兴趣的:(C#)