- 建立SQL連線
- 利用adapter將資料到入DataTable
- 判斷DataTable的某欄位資料是否匹配
- 決定是否跳轉到特定頁面
由於會使用到ADO.NET
務必要記得要用「using System.Data.SqlClient;」
會回傳boolean的值(階段1~3)
protected bool isProjectStop() { string connStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["連線字串"].ConnectionString; string strSQL = @"產生資料集的SQL語法"; DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connStr)) { //傳參數避免Injection和型態轉換錯誤 SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(strSQL, conn); da.SelectCommand = cmd; cmd.Parameters.Add("@變數", SqlDbType.VarChar, 12).Value =Request.QueryString["參數名"];//用「@變數」要篩選的條件 da.Fill(dt);//將DataAdapter篩選後的資料放入DataTable }
//依照欄位內容, 去回傳false, true if (dt.Rows[0]["欄位名"].ToString() == "S") return false; else return true;}利用boolean去執行(階段4)
if (isProjectStop()) //判斷案號是否停用, 是則導到指定頁 Response.Redirect("Default.aspx", true);
沒有留言:
張貼留言