因为项目需要,使用vs2008+Oracle开发,特意写了个例子供其他成员参考

笔者认为Enterprise Library 3.1 – May
2007提供的功能过于强大,可能项目里很多不需要.所以想简化使用

本例子是Oracle中的 orcl数据库

步骤一. 安装Enterprise Library 3.1 – May 2007

webconfig

步骤二.建立你的项目.建立bin目录.引入Microsoft.Practices.EnterpriseLibrary.Data.dll

  <dataConfiguration defaultDatabase=”ORACLE”>
    <providerMappings>
      <add
databaseType=”Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase,
Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35″
        name=”System.Data.OracleClient” />
    </providerMappings>
  </dataConfiguration>
  <connectionStrings>
    <add name=”ORACLE” connectionString=”Data Source=orcl;Persist
Security Info=True;User ID=scott;Password=scott;Unicode=True”
      providerName=”System.Data.OracleClient” />
  </connectionStrings>
  <appSettings />

Microsoft.Practices.EnterpriseLibrary.Common.dll

自己建立的一个公共连接类,大家可以拓展

步骤三.在web.config文件里建立一项

EDataCommon.cs

<connectionStrings>
    <add name=”myconn” connectionString=”Data
Source=SEE-CLN-059\PMSERVER;Initial Catalog=test;User
ID=sa;Password=”
      providerName=”System.Data.SqlClient” />
  </connectionStrings>

 using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data; 

 

public DataSet getDataSet(string sql)
    {
        DataSet ds = null;
        try
        {
            db = DatabaseFactory.CreateDatabase();
            ds = db.ExecuteDataSet(CommandType.Text, sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return ds;
    }

下面只展示如何读出数据的例子.其他跟你自己写类差不多效果啦

页面后台程序,

 

Index.aspx.cs

 

public partial class connect : System.Web.UI.Page
{
  

<%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”enterprisetest.aspx.cs” Inherits=”enterprisetest” %>

    protected void Page_Load(object sender, EventArgs e)
    {
      
        try
        {
            this.GridView1.DataSource = new
EDataCommon().getDataSet(“select * from dept;”).Tables[0];
            this.GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

        }

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>无标题页</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:GridView ID=”GridView1″ runat=”server”>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

    }
}

 

如果大家有任何疑问请及时联系.. 🙂 其实够简单的啦

 

codebehide

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;

public partial class enterprisetest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Database db = null;
        try
        {

            string strconn = “myconn”;
            db = DatabaseFactory.CreateDatabase(strconn);
            DataSet ds = db.ExecuteDataSet(CommandType.Text, “select  
Class_ID,Class_Name,Class_Pid   from   TC_Class   order   by  
Class_ID “);
            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());

        }

       
    }
}

 

漏了一点提示.必须建立连接字符串在web.config里,否则会抛出异常.. 🙂

 

 

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注