using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//----自己寫的(宣告)----
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
//----自己寫的(宣告)----
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//連接字串
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
//宣告DataReader
SqlDataReader dr = null;
//資料庫執行指令,搭配參數法
SqlCommand cmd = new SqlCommand("Select id,test_time,summary,author from test where id = @id", Conn);
//檢查送過來的是否為數字
if (IsNumeric(Request["id"]))
{
//搭配參數法
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(Request["id"]));
}
else
{
Response.Write("<h2>URL網址傳來的 id 並非數字!</h2>");
return;
}
try
{
//打開連線字串
Conn.Open();
//DataReader執行
dr = cmd.ExecuteReader();
//執行完丟給GV
GridView1.DataSource = dr;
//資料與GV綁定
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("<b>Error Message---- </b>" + ex.ToString() + "<HR/>");
}
finally
{
//關閉DR
if (dr != null)
{
cmd.Cancel();
dr.Close();
}
//關閉連線字串
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
Conn.Dispose();
}
//GV資源釋放
GridView1.Dispose();
}
}
}
留言列表