.net 組合查詢
阿新 • • 發佈:2017-08-09
方便 number foreach ndt 拼接 use int32 -- css
首先寫HTML布局:
<style type="text/css"> #tab1 { width: 100%; text-align: center; background-color: navy; border-spacing: 1px; /*邊框之間的空隙變成1像素*/ } #tab1 thead tr { color: white; } #tab1 tbody tr { background-color: white; } #tab1 td { padding: 7px 5px; } #tab1 tbody tr:hover { background-color: #f00; /*//鼠標移入時候的背景顏色*/ } </style> </head> <body> <form id="form1" runat="server"> <div> <table id="tab1"> <br /> 昵稱:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 性別:<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="true">全部</asp:ListItem> <asp:ListItem >男</asp:ListItem> <asp:ListItem>女</asp:ListItem> </asp:DropDownList> 生日:<asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem Value="true">全部</asp:ListItem> <asp:ListItem Value=">=">大於等於</asp:ListItem> <asp:ListItem Value="<=">小於等於</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="查詢" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /> <br /> <br /> <thead> <tr> <td>ID</td> <td>編號</td> <td>用戶名</td> <td>密碼</td> <td>昵稱</td> <td>性別</td> <td>生日</td> <td>民族</td> </tr> </thead> <tbody> <asp:Repeater ID="Repeater1" runat="server"> <%-- 在內容裏加Repeater和TtemTemplate--%> <ItemTemplate> <tr> <td><%#Eval("ID") %></td> <td><%#Eval("ucode") %></td> <td><%#Eval("username") %></td> <td><%#Eval("password") %></td> <td><%#Eval("nickname") %></td> <td><%#Eval("sexstr") %> <%-- <img src="<%#Eval("sexImg") %>" />--%> <%--使用圖片當性別區分--%> </td> <td><%#Eval("birthday","{0:yyyy年MM月dd日}") %></td> <%-- 日期的轉換--%> <td><%#Eval("NationName") %></td> </tr> </ItemTemplate> </asp:Repeater> </tbody> </table> </div> </form> </body> </html>
然後後臺代碼:
protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if(!IsPostBack) { Repeater1.DataSource = new UsersData().SelectAll(); Repeater1.DataBind(); } } void Button1_Click(object sender, EventArgs e) { //獲取要查詢的條件,拼接sql語句 int count = 0; string sql = "select *from Users"; Hashtable hs = new Hashtable(); if(TextBox1.Text.Trim().Length>0) { sql += " where nickname like @a"; hs.Add("@a","%"+TextBox1.Text.Trim()+"%"); count++; } if (DropDownList1.SelectedIndex!=0 ) //索引為0就是 不選全部 { if (count > 0) { sql += " and sex =" + (DropDownList1.SelectedValue == "男" ? "1" : "0"); } else { sql += " where sex =" + (DropDownList1.SelectedValue == "男" ? "1" : "0"); } count++; } if ( TextBox2.Text.Trim().Length > 0) { if (count > 0) { sql += " and Year(birthday)" + DropDownList2.SelectedValue + TextBox2.Text; } else { sql += " where Year(birthday)" + DropDownList2.SelectedValue + TextBox2.Text; } count++; } //Label1.Text = sql; //測試用的 //執行語句並接收返回值綁定,展示數據; Repeater1.DataSource = new UsersData().Selectsql(sql, hs); Repeater1.DataBind(); }
用到的方法:
public List<Users> SelectAll() { List<Users> ulist = new List<Users>(); cmd.CommandText = "select *from Users"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Users u = new Users(); u.ID = Convert.ToInt32(dr[0]); u.ucode = dr[1].ToString(); u.username = dr[2].ToString(); u.password = dr[3].ToString(); u.nickname = dr[4].ToString(); u.sex = Convert.ToBoolean(dr[5]); u.birthday = Convert.ToDateTime(dr[6]); u.nation = dr[7].ToString(); ulist.Add(u); } conn.Close(); return ulist; } public List<Users> Selectcount(int NowNumber, int Count) //NowNumber當前要看的頁數,Count當前頁的條數 { List<Users> ulist = new List<Users>(); cmd.CommandText = "select top " + Count + " *from Users where ID not in(select top " + ((NowNumber - 1) * Count) + " ID from Users)"; //分頁方法 ,把count放進去,查詢前幾條。後面也頁數條數,就是前去前面的頁數的條數。 conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Users u = new Users(); u.ID = Convert.ToInt32(dr[0]); u.ucode = dr[1].ToString(); u.username = dr[2].ToString(); u.password = dr[3].ToString(); u.nickname = dr[4].ToString(); u.sex = Convert.ToBoolean(dr[5]); u.birthday = Convert.ToDateTime(dr[6]); u.nation = dr[7].ToString(); ulist.Add(u); } conn.Close(); return ulist; } public List<Users> Selectsql(string sql,Hashtable hs) { List<Users> ulist = new List<Users>(); cmd.CommandText = sql; cmd.Parameters.Clear(); foreach(string s in hs.Keys) { cmd.Parameters.AddWithValue(s,hs[s]); } conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Users u = new Users(); u.ID = Convert.ToInt32(dr["ID"]); u.ucode = dr["ucode"].ToString(); u.username = dr["username"].ToString(); u.password = dr["password"].ToString(); u.nickname = dr["nickname"].ToString(); u.sex = Convert.ToBoolean(dr["sex"]); u.birthday = Convert.ToDateTime(dr["birthday"]); u.nation = dr["nation"].ToString(); ulist.Add(u); } conn.Close(); return ulist; }
所有的的理解都寫在了代碼裏面,方便下次看的時候理解。
.net 組合查詢