给你个完整例子:更想起情况,联系yahoo通:zizhao_chen

解决方案 »

  1.   

    using System;
    using System.ComponentModel;
    using System.Collections;
    using System.Diagnostics;namespace Org.Lacl.Lacl
    {
    /// <summary>
    /// utility 的摘要说明。
    /// </summary>
    public class Utility : System.ComponentModel.Component
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Utility(System.ComponentModel.IContainer container)
    {
    ///
    /// Windows.Forms 类撰写设计器支持所必需的
    ///
    container.Add(this);
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } public Utility()
    {
    ///
    /// Windows.Forms 类撰写设计器支持所必需的
    ///
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region 组件设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    { }
    #endregion public System.Data.IDbConnection GetDBConnection()
    {
    return new Npgsql.NpgsqlConnection(Configuration.NpgsqlConnectionString);;
    }
    }
    }
      

  2.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Odbc;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace Org.Lacl.Lacl
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class Default: System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.HyperLink HyperLink1;
    protected System.Web.UI.WebControls.HyperLink HyperLink2;
    protected System.Web.UI.WebControls.HyperLink HyperLink3;
    protected System.Web.UI.WebControls.HyperLink HyperLink4;
    protected Npgsql.NpgsqlConnection ConnectionLacl;
    protected Npgsql.NpgsqlCommand CommandLacl;
    protected Npgsql.NpgsqlDataReader DataReaderLacl; protected System.Web.UI.WebControls.DataList DataList1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    try
    {
    this.ConnectionLacl.ConnectionString=Configuration.NpgsqlConnectionString;
    this.ConnectionLacl.Open();
    this.CommandLacl.CommandText="select announcement_title,announcement_content,announcement_datetime,announcement_auther,announcement_id,announcement_isfront from announcement where announcement_isfront=TRUE order by announcement_datetime desc";
    DataReaderLacl=this.CommandLacl.ExecuteReader();
    this.DataList1.DataSource=DataReaderLacl;
    this.DataList1.DataBind();
    }
    catch(Exception ee)
    {
    Page.RegisterStartupScript("conncetionExcetion","<script>alert('连接数据库错误:\n"+ee.Message+"')</script>");
    }
    finally
    {
    this.ConnectionLacl.Close();
    }
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.ConnectionLacl = new Npgsql.NpgsqlConnection();
    this.CommandLacl = new Npgsql.NpgsqlCommand();
    // 
    // CommandLacl
    // 
    this.CommandLacl.Connection = this.ConnectionLacl;
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }