日期:2014-05-17  浏览次数:20826 次

C# 连接Oracle

 

 

    public static DataSet OracleExecDataSet(string sqlstr,List<OracleParameter> paras)
    {
        using (OracleConnection conn = new OracleConnection())
        {
            using (OracleCommand cmd = new OracleCommand())
            {
                try
                {
                    conn.ConnectionString = myOracleConnStr; //这里是链接字符串
                    cmd.Connection = conn;
                    cmd.CommandText = sqlstr;
                    foreach (OracleParameter pa in paras)
                    {
                        if (pa.Value != null)
                        {
                            cmd.Parameters.Add(pa);
                        }
                    }                  
                    using (OracleDataAdapter da = new OracleDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        cmd.Parameters.Clear();
                        return ds;
                    }
                }
                catch (Exception ex)
                {
                    InsertErrorLog(ex.Message);
                    cmd.Parameters.Clear();
                } 

            }
        }
        return null;        
       
    }


 

        //Oracle参数名使用 :ParaName
        DataSet ds = new DataSet();
        List<OracleParameter> paras = new List<OracleParameter>();
        paras.Add(new OracleParameter(":NAME", "AA"));
        string sqlstr = "select * from TB_NAME where NAME=:NAME";
        ds=  Sqlexec.OracleExecDataSet(sqlstr, paras);


 

  <add name="OracleConnectionString" connectionString="data source = test;user id= testuser; password=123456;persist security info=false"/>