asp.net 查詢sql數據表的網頁模板

最近由於工做需求,要製做一個網頁模板,主要是用於快速開發,能夠查詢Sql數據表信息的模板,html

昨天作好了,這個只是一個Demo,可是功能已經齊全了,前端

開發新的網站時,須要新增一個xml,複製粘貼網頁的前端和後臺,注意,前端的樣式和邏輯仍是要分開寫的。web

這樣,基本上在10分鐘之內,就能開發一個新的網頁(只有核心的查詢功能)。sql

 

這是效果圖:數據庫

下面記錄一下這個Demo:app

首先是要在Web項目的web.config中配置數據庫的鏈接字符串和xml文件名:ide

1 <appSettings>
2     <add key="xmlDir" value="xml"/>
3   </appSettings>
4   <connectionStrings>
5     <add name="connStr" connectionString="Data Source=10.76.25.XX;DATABASE=XXX;Persist Security Info=True;User ID=sa;Password=XXXXXX;Max Pool Size=300;" providerName="System.Data.SqlClient"/>
6 </connectionStrings>


接着,我有作好了一個幫助類QueryHelper:網站

 1 using System;  2 using System.Collections.Generic;  3 using System.Configuration;  4 using System.Data;  5 using System.Data.SqlClient;  6 using System.IO;  7 using System.Web.UI;  8 using System.Web.UI.HtmlControls;  9 using System.Web.UI.WebControls;  10 using System.Xml.Linq;  11 
 12 namespace Cong.Utility  13 {  14     public delegate void DelegatePageIndexChange(object obj, GridViewPageEventArgs e);  15 
 16     public class QueryHelper  17  {  18         private string xmlName { get; set; }  19         private Page page { get; set; }  20         private GridView gv { get; set; }  21 
 22         private DelegatePageIndexChange dele;  23 
 24         public QueryHelper(string xmlName, Page page, DelegatePageIndexChange dele)  25  {  26             this.xmlName = xmlName;  27             this.page = page;  28             this.gv = page.FindControl("gv") as GridView;  29             this.dele = dele;  30 
 31             gv.PageIndexChanging += Gv_PageIndexChanging;  32  }  33 
 34         public void BindData(object objIndex)  35  {  36             var xmlDir = ConfigurationManager.AppSettings["xmlDir"];  37             //獲取xml節點
 38             string path = AppDomain.CurrentDomain.BaseDirectory + (xmlDir == null ? "" : (xmlDir.ToString() + "\\")) + xmlName;  39             if (!File.Exists(path))  40                 throw new Exception("xml is not exist.");  41 
 42             XDocument xdoc = XDocument.Load(path);  43             XElement root = xdoc.Root;  44 
 45             //獲取基本查詢語句的變量
 46             string tableName = root.Element("tableName").Value;  47             string topNumber = root.Element("top").Value;  48 
 49             //拼接where條件語句
 50             List<string> conditions = new List<string>();  51             List<SqlParameter> ps = new List<SqlParameter>();  52             foreach (var ele in root.Element("field").Elements())  53  {  54                 if (ele.Element("search").Value.Equals("0"))  55  {  56                     HtmlInputText tb = page.FindControl("search_" + ele.Name.ToString()) as HtmlInputText;  57                     if (tb == null)  58                         continue;  59 
 60                     if (!tb.Value.Trim().Equals(""))  61  {  62                         conditions.Add(string.Format("{0} like '%'+@{0}+'%'", ele.Name.ToString()));  63                         ps.Add(new SqlParameter("@" + ele.Name.ToString(), tb.Value.Trim()));  64  }  65  }  66  }  67             //是否有刪除標識
 68             foreach (var ele in root.Element("flag").Elements())  69  {  70                 if (ele.Value.Equals("0"))  71                     conditions.Add(ele.Name + " " + ele.Value);  72  }  73             string strWhere = "";  74             if (conditions.Count > 0)  75                 strWhere = "where " + string.Join(" and ", conditions.ToArray());  76 
 77             //拼接sort條件語句
 78             List<string> sorts = new List<string>();  79             foreach (var ele in root.Element("sorts").Elements())  80  {  81                 sorts.Add(ele.Element("field").Value + " " + ele.Element("type").Value);  82  }  83             string strOrderBy = "";  84             if (sorts.Count > 0)  85                 strOrderBy = "order by " + string.Join(",", sorts.ToArray());  86 
 87             //拼接sql語句
 88             string sql = string.Format("select top {0} * from {1} {2} {3}", topNumber, tableName, strWhere, strOrderBy);  89             DataTable dt = SqlHelper.ExecuteDataTable(sql, ps);  90 
 91  BindGV(dt, root, objIndex);  92  }  93 
 94         public void BindGV(DataTable dt, XElement root, object objIndex)  95  {  96             gv.DataSource = null;  97  gv.Columns.Clear();  98             gv.AutoGenerateColumns = false;  99             gv.AllowPaging = true; 100             gv.PageSize = int.Parse(root.Element("pageSize").Value); 101 
102             BoundField gvColumn = null; 103             foreach (var ele in root.Element("field").Elements()) 104  { 105                 if (ele.Element("visible").Value.Equals("0")) 106  { 107                     gvColumn = new BoundField(); 108                     gvColumn.HeaderText = ele.Element("cn").Value; 109                     gvColumn.DataField = ele.Name.ToString(); 110  gv.Columns.Add(gvColumn); 111                     gvColumn = null; 112  } 113  } 114 
115             gv.DataSource = dt; 116             gv.PageIndex = objIndex == null ? 0 : Convert.ToInt32(objIndex); 117  gv.DataBind(); 118  } 119 
120         private void Gv_PageIndexChanging(object sender, GridViewPageEventArgs e) 121  { 122  dele(sender, e); 123  BindData(e.NewPageIndex); 124  } 125  } 126 }


而後,是SqlHelper,這個就不放出來了,之前也發過。ui

 

接着建立一個xml,我是放在項目主目錄下的「xml」文件夾內,內容以下:this

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <root>
 3   <tableName>V_121_LOT_LIST</tableName>
 4   <top>100</top>
 5   <field>
 6     <lot_id>
 7       <cn>Id</cn>
 8       <visible>0</visible>
 9       <search>0</search>
10     </lot_id>
11     <lot_code>
12       <cn>批號</cn>
13       <visible>0</visible>
14       <search>0</search>
15     </lot_code>
16     <plan_qty>
17       <cn>生產數量</cn>
18       <visible>1</visible>
19       <search>1</search>
20     </plan_qty>
21     <now_qty>
22       <cn>當前數量</cn>
23       <visible>0</visible>
24       <search>1</search>
25     </now_qty>
26   </field>
27   <flag>
28     <state_flg>1</state_flg>
29     <del_flg>1</del_flg>
30   </flag>
31   <sorts>
32     <sort>
33       <field>lot_id</field>
34       <type>desc</type>
35     </sort>
36   </sorts>
37   <pageSize>20</pageSize>
38 </root>


最後,建立一個WebForm.aspx,

下面分別是後臺和前端的代碼:

 1 using Cong.Utility;  2 using System;  3 using System.Web.UI.WebControls;  4 
 5 namespace QueryTemplet  6 {  7     public partial class lot : System.Web.UI.Page  8  {  9         private static string xmlName = "lot.xml"; 10 
11         protected QueryHelper qh = null; 12 
13         protected void Page_Load(object sender, EventArgs e) 14  { 15             qh = new QueryHelper(xmlName, this, Gv_PageIndexChanging); 16             qh.BindData(ViewState["pageIndex"]); 17  } 18 
19         private void Gv_PageIndexChanging(object sender, GridViewPageEventArgs e) 20  { 21             ViewState["pageIndex"] = e.NewPageIndex; 22  } 23 
24         protected void btnSearch_Click(object sender, EventArgs e) 25  { 26             qh.BindData(ViewState["pageIndex"]); 27  } 28  } 29 }
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="lot.aspx.cs" Inherits="QueryTemplet.lot" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <div>
13             Id:<input type="text" id="search_lot_id" runat="server" />
14             批號:<input type="text" id="search_lot_code" runat="server" />
15             <br />
16             <asp:Button Text="查找" ID="btnSearch" runat="server" OnClick="btnSearch_Click" />
17         </div>
18         <div>
19             <asp:GridView ID="gv" runat="server">
20             </asp:GridView>
21         </div>
22     </form>
23 </body>
24 </html>

 

前端的代碼,本身再套上樣式和編寫js代碼就能夠使用了。


最後,也放出另外一個效果圖,我作好這個新的頁面,只複製粘貼了幾分鐘。

就這些,歡迎交流。

 

源碼下載:http://download.csdn.net/detail/cycong108/9739562

相關文章
相關標籤/搜索