1. 程式人生 > 實用技巧 >用連結串列實現揹包,佇列,棧

用連結串列實現揹包,佇列,棧

 public class LayUiTableInfo
    {
        public int code { get; set; }
        public string msg { get; set; }
        public int count { get; set; }
        public DataTable data { get; set; }
    }

第二個方法是網上找的, 修改了一下,可以直接碰到複雜一點的聯查 ,還有前端的搜尋,總之就是拼接好sql語句 方法2 效率高於是就用了方法2

 /// <summary>
        ///
分頁查詢函式 /// </summary> /// <param name="connStr">資料庫連線字串</param> /// <param name="strSql">sql語句</param> /// <param name="Params">引數</param> /// <param name="pageSize">每頁條數</param> /// <param name="pageIndex">頁碼</param>
/// <param name="order">排序欄位</param> /// <param name="sort">排序放</param> /// <returns>備註:查詢效率偏低</returns> public DataTable Pagination(string strSql, List<SqlParameter> Params, int pageSize, int pageIndex, string order, string sort) { SqlConnection conn
= new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(); SqlDataAdapter ada = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); cmd.Connection = conn; string sqlFinal = string.Format(@"WITH tmp2 as( SELECT ROW_NUMBER() OVER(ORDER BY {1} {2}) AS rownum,* from ({0}) tmp1) select (select count(*) from tmp2)total,* from tmp2 where tmp2.rownum BETWEEN @startRow and @endRow", strSql, order, sort); Params.Add(new SqlParameter("@startRow", (pageIndex - 1) * pageSize + 1) { SqlDbType = SqlDbType.Int }); Params.Add(new SqlParameter("@endRow", pageIndex * pageSize) { SqlDbType = SqlDbType.Int }); cmd.CommandText = sqlFinal; for (int i = 0; i < Params.Count; i++) { cmd.Parameters.Add(Params[i]); } ada.Fill(dt); return dt; } /// <summary> /// 分頁查詢函式 /// </summary> /// <param name="connStr">資料庫連線字串</param> /// <param name="strSql">sql語句</param> /// <param name="Params">引數</param> /// <param name="pageSize">每頁條數</param> /// <param name="pageIndex">頁碼</param> /// <param name="order">排序欄位</param> /// <param name="sort">排序放</param> /// <returns>備註:查詢效率高,但是會建臨時表</returns> public static DataTable Pagination2(string strSql, int pageSize, int pageIndex, string order, string sort) { SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(); SqlDataAdapter ada = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); cmd.Connection = conn; string sqlFinal = string.Format(@"SELECT ROW_NUMBER() OVER(ORDER BY {1} {2}) AS rownum,* into #tmp2 from ({0}) tmp1; select (select count(*) from #tmp2)total,* from #tmp2 where #tmp2.rownum BETWEEN @startRow and @endRow", strSql, order, sort); List<SqlParameter> Params = new List<SqlParameter>(); Params.Add(new SqlParameter("@startRow", (pageIndex - 1) * pageSize + 1) { SqlDbType = SqlDbType.Int }); Params.Add(new SqlParameter("@endRow", pageIndex * pageSize) { SqlDbType = SqlDbType.Int }); cmd.CommandText = sqlFinal; for (int i = 0; i < Params.Count; i++) { cmd.Parameters.Add(Params[i]); } ada.Fill(dt); return dt; }

三,引用config

static string connStr = ConfigurationManager.ConnectionStrings["CMSConnectionString"].ToString();

四 控制器端 加搜尋,功能

   [HttpGet]
        public IHttpActionResult TerminalList()
        {
            object rt = null;
            //if (AuthenticationHelper.IsAuthenticated())
            //{
            try
            {
                int page = QueryHelper.GetInteger("page", 1);
                //每頁條數
                int limit = QueryHelper.GetInteger("limit", 10);
                //排序欄位
                string field = QueryHelper.GetString("field", string.Empty);
                //排序方式
                string order = QueryHelper.GetString("order", string.Empty);
                //返回列名
                string columns = "*";
                //表名
                string tablename = "AutoWscl_Terminal";
                string orderby = "TerminalID";
                string where = " 1=1  ";
                string terminalName = QueryHelper.GetString("terminalName", string.Empty);
                string portType = QueryHelper.GetString("portType", string.Empty);
                string messageFlag = QueryHelper.GetString("messageFlag", string.Empty);
                int isMaster = QueryHelper.GetInteger("isMaster", -1);
                //裝置名
                if (!string.IsNullOrEmpty(terminalName))
                {
                    where += " and terminalName like N'%" + SqlHelper.EscapeLikeText(terminalName) + "%'";
                }
                //裝置型別
                if (!string.IsNullOrEmpty(portType))
                {
                    where += " and portType='" + SqlHelper.EscapeQuotes(portType) + "'";
                }
                //裝置標識
                if (!string.IsNullOrEmpty(messageFlag))
                {
                    where += " and  MessageFlag like N'%" + SqlHelper.EscapeLikeText(SqlHelper.EscapeQuotes(messageFlag)) + "%'";
                }
                //是否是主裝置
                if (isMaster != -1)
                {
                    where += " and isMaster=" +isMaster;
                }

                if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
                {
                    orderby = field + " " + order;
                }
                rt =new TerminalService().GetTableJson(tablename, columns, where, orderby, page, limit);
            }
            catch (Exception ex)
            {
                rt = ex.Message;
            }
            //}
            //else 
            //{
            //    rt = "請重新登陸!";
            //}
            return Json(rt);
        }

五 前端 layui

Html Body: <div class="layui-form">
    <!--搜尋條-->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">裝置管理</label>
            <div class="layui-input-inline">
                <input type="text" autocomplete="off" id="terminalName" required  lay-verify="required" placeholder="請裝置名稱" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select id="isMaster" width="20px">
                    <option value='' disabled selected style='display:none;'>是否是主裝置</option>
                    <option value='1'></option>
                    <option value='0'></option>
                </select>
            </div>
        </div>
        <!--   <div class="layui-inline">
               <label class="layui-form-label">活動狀態</label>
               <div class="layui-input-inline">
                   <select id="Status" lay-verify="required">
                       <option value="">全部</option>
                       <option value="0">未啟用</option>
                       <option value="1">啟用</option>
                   </select>
               </div>
           </div>-->
        <div class="layui-inline">
            <button class="layui-btn" data-type="chaxun" id="chaxun">查詢</button>
        </div>
    </div><!--搜尋條結束-->
    <div>

        <button class="layui-btn" data-type="addNew" id="addNew">新增新地址</button>
    </div>
    <table class="layui-hide" id="LAY_table_user" lay-filter="user"></table>
</div>

Html Footer: <script src="xxxxy/jquery-2.2.2.min.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">編輯</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">刪除</a>
</script>
<script>
    layui.use('table', function(){
        var table = layui.table;
        var $ = layui.$;//等同於jquery

        //方法級渲染
        table.render({
            elem: '#LAY_table_user'//table元素的ID
            ,id: 'testReload'//容器的ID
            ,url: '/xxxxrminalList'
            ,height:$(window).height()/2+80
            , cols: [[
              xxx
                { field: "terminalCMD", title: "裝置命令", width: 120 },
                { field: "messageFlag", title: "裝置標識", width: 200 },
                { field: "parentlocation", title: "位置", width: 200 },
                { field: "LocationName", title: "採集點", width: 200 },
                { title: "操作", width: 180, align: "center", fixed: "right", templet: "#barDemo"}
            ]]
            ,page: true
            ,limits: [10,20,30]
            ,limit: 10
            ,where: {
                terminalName:$('#terminalName').val(),
                isMaster:$('#isMaster').val()
            }
        });

        //監聽工具條
        table.on('tool(user)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                window.location.href="/Mxxxxl/edit.aspx?TerminalID="+data.TerminalID;
            }
            else if(obj.event === 'del'){
                layer.confirm('確定要刪除該地址?', function(index){
                    //do something
                    deleteActive(data.TerminalID,table);
                    layer.close(index);
                });
            }
         
        });
        //監聽工具條結束

        //監聽排序
        table.on('sort(user)', function(obj){ //注:tool是工具條事件名,test是table原始容器的屬性 lay-filter="對應的值"
            table.reload('testReload', {//重新整理列表
                initSort: obj //記錄初始排序,如果不設的話,將無法標記表頭的排序狀態。 layui 2.1.1 新增引數
                ,where: { //請求引數
                    field: obj.field //排序欄位
                    ,order: obj.type //排序方式
                }
            });
        });
        //監聽排序結束

        //查詢
        $("#chaxun").click(function(){
            table.reload('testReload', {//重新整理列表
                where: {
                    terminalName:$('#terminalName').val(),
                    isMaster:$('#isMaster').val()
                },page: {
                    curr: 1 //重新從第 1 頁開始
                }
            });
        })
        $("#addNew").click(function(){
            window.location.href="/Managexx.aspx";
        })

    });


    function deleteActive(TerminalID,table){
        var indexload = layer.load(1, {
            shade: [0.3,'#000'],
            success: function(layero, indexload){
                $.ajax({
                    type: 'Get',
                    async: false,
                    dataType: 'json',
                    url: '/xxal/DelTerminal',
                    data: { TerminalID: TerminalID},//$("form").serialize(),
                    /* processData: false,  // 告訴jQuery不要去處理髮送的資料
                     contentType: false,  // 告訴jQuery不要去設定Content-Type請求頭*/
                    success: function (data) {
                        if(data.res == "ok"){
                            layer.alert("刪除成功!", function(index){
                                layer.close(index);
                                layer.close(indexload);
                                table.reload('testReload', {
                                    where: {
                                        terminalName:$('#terminalName').val()
                                        ,Status:$('#Status').val()
                                    }
                                });
                            });
                        }
                        else{
                            layer.alert(data.res, function(index){
                                layer.close(index);
                                layer.close(indexload);
                                table.reload('testReload', {
                                    where: {
                                        terminalName:$('#terminalName').val()
                                        , isMaster:$('#isMaster').val()
                                    }
                                });
                            });
                        }
                    },
                    error:function(e){
                        layer.alert("刪除失敗,請重試!", function(index){
                            //do something
                            layer.close(index);
                            layer.close(indexload);
                            table.reload('testReload', {
                                where: {
                                    terminalName:$('#terminalName').val()
                                    , isMaster:$('#isMaster').val()
                                }
                            });
                        });
                    }
                });
            }
        });

    }

  

</script>

service 端就是些sql呼叫分頁方法的 忽略