SQLHelper(oracle)

using System;
using System.Data.OleDb;
using System.Data.Common;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace DataConvert.Lib
{
    public static class SQLHelper
    {

        /// 
        /// 获取数据
        /// 
        /// SQL
        /// 连接名称
        /// DataSet
        public static DataSet GetDBDataSet(string cmdtxt, string DBType)
        {
            DataSet dsOrder = new DataSet();
            ConnectionStringSettings sDB = ConfigurationManager.ConnectionStrings[DBType];
            if (sDB.ProviderName != "")
            {
                DbProviderFactory fDB = DbProviderFactories.GetFactory(sDB.ProviderName);
                using (DbConnection connDB = fDB.CreateConnection())
                {
                    connDB.ConnectionString = sDB.ConnectionString;
                    try
                    {
                        connDB.Open();
                        DbCommand cmdDB = connDB.CreateCommand();
                        cmdDB.CommandText = cmdtxt;
                        DbDataAdapter da = fDB.CreateDataAdapter();
                        da.SelectCommand = cmdDB;
                        da.Fill(dsOrder);
                        connDB.Close();
                    }
                    catch (Exception ex)
                    {
                        LOG.WriteLog(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss:fff]") + "   " + ex.ToString());
                        connDB.Close();
                    }
                }
            }
            else
            {
                using (OleDbConnection connOleDb = new OleDbConnection(sDB.ConnectionString))
                {
                    try
                    {
                        connOleDb.Open();
                        OleDbCommand commOleDb = connOleDb.CreateCommand();
                        OleDbDataAdapter daOleDb = new OleDbDataAdapter();
                        commOleDb.CommandText = cmdtxt;
                        daOleDb.SelectCommand = commOleDb;
                        daOleDb.Fill(dsOrder);
                        connOleDb.Close();
                    }
                    catch (Exception e)
                    {
                        LOG.WriteLog(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss:fff]") + "   " + e.ToString());
                        connOleDb.Close();
                    }
                }
            }
            return dsOrder;
        }


        /// 
        /// 执行上传SQL
        /// 
        /// SQL
        /// 连接名称
        /// 
        public static string ExcuteSQL(string cmdtxt, string DBType)
        {
            string str = string.Empty;
            ConnectionStringSettings sDB = ConfigurationManager.ConnectionStrings[DBType];
            if (sDB.ProviderName != "")
            {
                DbProviderFactory fDB = DbProviderFactories.GetFactory(sDB.ProviderName);
                using (DbConnection connDB = fDB.CreateConnection())
                {
                    connDB.ConnectionString = sDB.ConnectionString;
                    try
                    {
                        connDB.Open();
                        DbCommand cmdDB = connDB.CreateCommand();
                        cmdDB.CommandText = cmdtxt;
                        cmdDB.ExecuteNonQuery();
                        connDB.Close();
                    }
                    catch (Exception ex)
                    {
                        LOG.WriteLog(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss:fff]") + "   " + ex.ToString());
                        connDB.Close();
                        str = ex.Message;
                    }
                }
            }
            else
            {
                using (OleDbConnection connOleDb = new OleDbConnection(sDB.ConnectionString))
                {
                    try
                    {
                        connOleDb.Open();
                        OleDbCommand commOleDb = connOleDb.CreateCommand();
                        commOleDb.CommandText = cmdtxt;
                        commOleDb.ExecuteNonQuery();
                        connOleDb.Close();
                    }
                    catch (Exception e)
                    {
                        LOG.WriteLog(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss:fff]") + "   " + e.ToString());
                        connOleDb.Close();
                        str = e.Message;
                    }
                }
            }
            return str;
        }
        public static string ExcuteByteSql(string cmdtxt, byte[] b)
        {
            string str = string.Empty;

            string cnnstr = "provider=OraOLEDB.Oracle;data source=*;User Id=*;Password=*;";

            //OleDbConnection con = new OleDbConnection(cnnstr);
            using (OleDbConnection con = new OleDbConnection(cnnstr))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(cnnstr, con);
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = cnnstr;

                    cmd.CommandText = cmdtxt;
                    cmd.Parameters.Add("p", System.Data.OleDb.OleDbType.Binary, b.Length);
                    cmd.Parameters["p"].Value = b;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception e)
                {
                    LOG.WriteLog(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss:fff]") + "   " + e.ToString());
                    con.Close();
                    str = e.Message;
                }
            }
            return str;
        }
    }
}

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