Enterprise Library 3.1 简化使用范例一
笔者认为Enterprise Library 3.1 - May 2007提供的功能过于强大,可能项目里很多不需要.所以想简化使用
步骤一. 安装Enterprise Library 3.1 - May 2007
步骤二.建立你的项目.建立bin目录.引入Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll
步骤三.在web.config文件里建立一项
<connectionStrings>
<add name="myconn" connectionString="Data Source=SEE-CLN-059\PMSERVER;Initial Catalog=test;User ID=sa;Password="
providerName="System.Data.SqlClient" />
</connectionStrings>
下面只展示如何读出数据的例子.其他跟你自己写类差不多效果啦
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="enterprisetest.aspx.cs" Inherits="enterprisetest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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里,否则会抛出异常.. :-)