数据库
首页 > 数据库> > C# Oracle DBHelper简版

C# Oracle DBHelper简版

作者:互联网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;

namespace Service
{
    public class DBHelper
    {
        //连接数据库的字符串两种方式
        //1.代码写死
        static string connstr = "Password=123456;Persist Security Info=True;User ID=Nessie;Data Source=ORA";
        //2.获取App.config配置
        //static string connstr = ConfigurationManager.ConnectionStrings["OracleConnString"].ConnectionString;

        public static DataTable GetTable(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                using (OracleConnection conn = new OracleConnection(connstr))
                {
                    OracleDataAdapter da = new OracleDataAdapter(sql, conn);
                    da.Fill(dt);
                }
            }
            catch (Exception )
            {
                return null;
            }
            return dt;
        }

        public static int ExeSql(string sql)
        {
            int ret = -1;
            try
            {
                using (OracleConnection conn = new OracleConnection(connstr))
                {
                    OracleCommand cmd = new OracleCommand(sql, conn);
                    conn.Open();
                    ret = cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
    }
}

附App.config配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="OracleConnString" connectionString="Data Source=ORA;User Id=Nessie;Password=123456;" providerName="Oracle.DataAccess.Client" />
  </connectionStrings>
</configuration>

标签:string,C#,System,DBHelper,new,Oracle,using,connstr,conn
来源: https://www.cnblogs.com/bing-xinyuan/p/12100446.html