1. 程式人生 > >Label控制元件的使用

Label控制元件的使用

該例項的功能是通過 Label控制元件顯示資料庫連線是否成功。

下面是原始碼:

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 System.Data.SqlClient;

public partial class WebSite1_Ex05_02 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<center><b>使用Label控制元件顯示資料庫連線是否成功</b></center>");
        string connString = "server=localhost;uid=sa;pwd=80712;database=Northwind";
        try
        {
            SqlConnection myConnection = new SqlConnection(connString);
            myConnection.Open();
            Label1.Text = "連線成功,連線字串是:" + connString;
        }
        catch
        {
            Label1.Text = "連線失敗,連線字串是:" + connString;
        }
    }
}