通過使用者選擇多個條件來進生相應的查詢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class All_Score : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null) //跳走
{
Server.Transfer("MangerAdmin.aspx");
}
else
{
Label3.Text = "";
if (!IsPostBack)
{
DataTable dt = Class1.exetable("select * from record");
Table_Records(dt); //表格建立
}
}
}
private void Table_Records(DataTable dt)
{
Cteate_Title(); //
TableRow row = new TableRow();
TableCell cell = new TableCell();
foreach (DataRow item in dt.Rows)
{
row = new TableRow();
for (int i = 1; i <= 7; i++)
{
cell = new TableCell();
cell.Text = item[i].ToString();
row.Cells.Add(cell);
if (i==1)
{
if(!DropDownList1.Items.Contains(new ListItem(item[i].ToString())))
DropDownList1.Items.Add(new ListItem(item[i].ToString()));
}
if (i == 2)
{
if (!DropDownList2.Items.Contains(new ListItem(item[i].ToString())))
DropDownList2.Items.Add(new ListItem(item[i].ToString()));
}
if(i==7)
{
if (!DropDownList3.Items.Contains(new ListItem(item[i].ToString())))
DropDownList3.Items.Add(new ListItem(item[i].ToString()));
}
}
Table1.Rows.Add(row);
}
} //表格
private void Cteate_Title()
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = "回答人";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "所回答的問題";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "正確答案";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "回答人答案";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "分值";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "回答日期";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "問題日期";
row.Cells.Add(cell);
Table1.Rows.Add(row);
} //表頭
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
FindRcord_Oper(); //使用者自定義查詢函式的呼叫
}
else
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('對不起!您還沒有啟用FindRecord');</script>");
DataTable dt = Class1.exetable("select * from record");
Table_Records(dt); //表格建立
}
}
private void FindRcord_Oper()
{
string checkbox = "";
checkbox = Create_Find_cmd(checkbox); //生成使用者選中的引數
if (checkbox.Length == 0)
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('您沒有選中!無法為您提供服務!');</script>");
}
else
{
string[] ckb = checkbox.Substring(1).Split(',');
//string[] sql_c = sqlcmd.Split(','); //分解欄位
string Sql_Where = "";
foreach (string item in ckb)
{
Sql_Where += "and " + item + "="; //串加器
if (item == "answername") // 篩選器 answername
{
Sql_Where +="'"+ DropDownList1.SelectedItem.Text + "' ";
}
else if (item == "answertitle") //篩選器answertitle
{
Sql_Where += "'" + DropDownList2.SelectedItem.Text + "' ";
}
else if (item == "ZhouId") //篩選器ZhouId
{
Sql_Where += "'"+DropDownList3.SelectedItem.Text + "' ";
}
}
string sqlcmd = "select * from record where " + Sql_Where.Substring(3);
DataTable dt = Class1.exetable(sqlcmd);
if (RadioButton2.Checked)
{
Table_Records(dt);
}
else
{
addcords(dt);
}
}
} //使用者自定義查詢函式
private string Create_Find_cmd(string checkbox)
{
if (CheckBox1.Checked)
{
checkbox += ",answername";
}
if (CheckBox2.Checked)
{
checkbox += ",answertitle";
}
if (CheckBox3.Checked)
{
checkbox += ",ZhouId";
}
return checkbox;
} //建立查詢欄位對像
protected void Button2_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
FindRcord_Oper();
}
else
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('對不起!您還沒有啟用FindScore');</script>");
DataTable dt = Class1.exetable("select * from record");
Table_Records(dt); //表格建立
}
//DataTable dt = Class1.exetable("select * from record where answername='"+DropDownList1.SelectedItem.Text+"'");
// addcords(dt);
}
//private void Oper_One()
//{
// string checkbox = "";
// checkbox = Create_Find_cmd(checkbox); //生成使用者選中的引數
// if (checkbox.Length == 0)
// {
// Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('您沒有選中!無法為您提供服務!');</script>");
// }
// else
// {
// string[] ckb = checkbox.Substring(1).Split(',');
// //string[] sql_c = sqlcmd.Split(','); //分解欄位
// string Sql_Where = "";
// foreach (string item in ckb)
// {
// Sql_Where += "and " + item + "="; //串加器
// if (item == "answername") // 篩選器 answername
// {
// Sql_Where += DropDownList1.SelectedItem.Text + " ";
// }
// else if (item == "answertitle") //篩選器answertitle
// {
// Sql_Where += "'" + DropDownList2.SelectedItem.Text + "' ";
// }
// else if (item == "ZhouId") //篩選器ZhouId
// {
// Sql_Where += DropDownList3.SelectedItem.Text + " ";
// }
// }
// string sqlcmd = "select * from record where " + Sql_Where.Substring(3);
// DataTable dt = Class1.exetable(sqlcmd);
// Table_Records(dt);
// }
//}
private void addcords(DataTable dt)
{
Table table = new Table();
int num = 0;
createtable_title(table); //建立表頭
foreach (DataRow item in dt.Rows)
{
if (!DropDownList1.Items.Contains(new ListItem(item[7].ToString())))
{
DropDownList1.Items.Add(new ListItem(item[7].ToString()));
}
TableRow row = new TableRow();
bool sign = false;
string score = "0";
for (int i = 1; i < 7; i++)
{
TableCell cell = new TableCell();
cell.Text = item[i].ToString();
cell.Width = 100;
if (i == 2)
{
cell.Width = 300;
}
row.Cells.Add(cell);
if (i == 5)
{
if (item[4].ToString() == item[3].ToString().Trim())
{
cell = new TableCell();
cell.Text = "正確";
cell.ForeColor = System.Drawing.Color.Blue;
row.Cells.Add(cell);
sign = true;
score = item[5].ToString(); //存分數
}
else
{
cell = new TableCell();
cell.Text = "錯誤";
cell.ForeColor = System.Drawing.Color.Red;
row.Cells.Add(cell);
score = "0";
}
}
if (i == 6)
{
if (sign)
{
cell = new TableCell();
cell.Text = score;
num += Convert.ToInt32(score);
cell.ForeColor = System.Drawing.Color.Black;
row.Cells.Add(cell);
}
else
{
cell = new TableCell();
cell.Text = score;
cell.ForeColor = System.Drawing.Color.Red;
row.Cells.Add(cell);
}
}
}
Table1.Rows.Add(row);
Label3.Text = num.ToString();
}
} //新增記錄
private void createtable_title(Table table)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = "回答者";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "問 題";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "正確答案";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "您的回答";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "問題分數";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "回答情況";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "回答日期";
cell.Width = 50;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "得分情況";
cell.Width = 50;
row.Cells.Add(cell);
row.ForeColor = System.Drawing.Color.Brown;
row.Font.Bold = true;
//table.Rows.Add(row);
//table.Width = 800;
Table1.Rows.Add(row);
}
}
相關文章
- PostgreSQL多查詢條件,多個索引的選擇演算法與問題診斷方法SQL索引演算法
- Laravel 多條件查詢Laravel
- SQL多條件查詢SQL
- mysql多條件過濾查詢之mysq高階查詢MySql
- 多條件查詢---ssh版本
- 記錄一篇關於條件查詢語句的用法:1.通過QueryWrapper進行條件構造2.通過Example進行條件構造APP
- 寫一個“特殊”的查詢構造器 – (四、條件查詢:複雜條件)
- Laravel 多條件查詢時粗心導致的一個 BUGLaravel
- SpringBoot Jpa多條件查詢Spring Boot
- mysql帶AND關鍵字的多條件查詢MySql
- mybatis多條件的模糊查詢解決方案MyBatis
- linq to sql的多條件動態查詢SQL
- SQL中多條件查詢括號的用途SQL
- 無合適where條件過濾時儘量選擇order by後的欄位以驅動表進行查詢薦
- MySQL全面瓦解7:查詢的過濾條件MySql
- 同一欄位多個查詢條件時遇到的一個問題
- MongoDB查詢條件MongoDB
- MongoDB條件查詢MongoDB
- mysql條件查詢MySql
- 基於Solr的HBase多條件查詢測試Solr
- Linq查詢之多個排序條件排序
- Linq兩個from查詢條件
- jsp+servlet+mysql多條件模糊查詢JSServletMySql
- MySQL in 查詢,並通過 FIELD 函式按照查詢條件順序返回結果MySql函式
- Spring data jpa 多表查詢(三:多對多關係動態條件查詢)Spring
- 條件查詢JSPJS
- MongoDB之資料查詢(where條件過濾)MongoDB
- JN專案-查詢條件過濾特殊字元字元
- 自定義 Azure Table storage 查詢過濾條件
- 通過外部表改進一個繁瑣的大查詢
- 一個NOT EXISTS含有OR條件子查詢的優化優化
- 查詢作為條件的SQLSQL
- 基於條件熵的特徵選擇熵特徵
- 如何通過程式來查詢表名
- 查詢/刪除重複的資料(單個欄位和多個欄位條件)
- mongodb條件查詢不等於MongoDB
- golang beego orm 查詢條件 or andGolangORM
- 【mybatis-plus】條件查詢MyBatis