ASP.NET Oracle数据库连接

1。方式一
using System.Data.OracleClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OracleConnection conn = new OracleConnection("Data Source=orcl;User ID=base;Password=base");
        OracleDataAdapter ad = new OracleDataAdapter("select * from time_frame", conn);

        DataSet ds = new DataSet();
        ad.Fill(ds, "t");

        foreach (DataRow dr in ds.Tables["t"].Rows) {

            Response.Write(dr["YEAR"].ToString() + "<br>");
        }

    }
}
2。方式二

<connectionStrings>
  <add name="OracleConn" connectionString="Data Source=orcl;User ID=base;Password=base"/>
 </connectionStrings>


using System.Data.OracleClient;

/// <summary>
/// Data 的摘要说明
/// </summary>
public class Data
{
    private string ConnString;

    /// <summary>
    ///
    /// </summary>
    /// <param name="strConnName">"OracleConn"</param>
 public Data(string strConnName)
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
         this.ConnString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[strConnName].ConnectionString;
 }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public DataSet QueryTimeFrame() {

      
        String strSql = "select * from time_frame";
        DataSet ds = new DataSet();
        ds = this.QueryData(strSql);
        return ds;

    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="strUser"></param>
    /// <returns></returns>
    public DataSet QueryOrganizationID(string strUser)
    {


        String strSql = "select * from sysuser where USERACCOUNT = '"+strUser+"'";
        DataSet ds = new DataSet();
        ds = this.QueryData(strSql);
        return ds;

    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns></returns>
    private DataSet QueryData(string strSql) {

        OracleConnection conn = new OracleConnection(this.ConnString);
        OracleDataAdapter dapt = new OracleDataAdapter(strSql, conn);
        DataSet ds = new DataSet();
        dapt.Fill(ds, "tab");
        return ds;

    }
}

posted @ 2006-11-28 14:20  疯一样的自由  阅读(765)  评论(1编辑  收藏  举报