Пример #1
0
        public static string ReadSetting(string name)
        {
            DataTable dt = MySqlHelper.ExecuteDataTable("select value from T_Settings where Name=@Name",
                                                        new MySqlParameter("@Name", name));

            if (dt.Rows.Count <= 0)
            {
                throw new Exception("找不到Name=" + name + "的配置项");
            }
            else if (dt.Rows.Count > 1)
            {
                throw new Exception("找到多条数据");
            }
            return((string)dt.Rows[0]["Value"]);
        }
Пример #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //todo:检验是否传递了Id,已经Id的合法性
            long      id = Convert.ToInt64(context.Request["Id"]);
            DataTable dt = MySqlHelper.ExecuteDataTable("select * from T_Product where Id=@Id", new MySqlParameter("@Id", id));

            if (dt.Rows.Count <= 0)
            {
                context.Response.Write("找不到");
            }
            else if (dt.Rows.Count > 1)
            {
                context.Response.Write("找到多条数据");
            }
            else
            {
                DataRow product = dt.Rows[0];
                var     data    = new { Product = product };
                string  html    = CommonHelper.RenderHtml("Front/ProductView.html", data);
                context.Response.Write(html);
            }
        }
Пример #3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            int pageNum = 1;

            if (context.Request["PageNum"] != null)
            {
                pageNum = Convert.ToInt32(context.Request["PageNum"]);
            }

            //如果指定了CategoryId参数,则只显示特定类型的产品
            //如果没有指定,则显示所有产品
            bool hasCategoryId = !string.IsNullOrEmpty(context.Request["CategoryId"]);

            if (hasCategoryId)
            {
                long categoryId = Convert.ToInt64(context.Request["CategoryId"]);
                products = MySqlHelper.ExecuteDataTable(@"select * from T_Product where CategoriyId=@CategoriyId limit 
                @Start, @End ", new MySqlParameter("@CategoriyId", categoryId),
                                                        new MySqlParameter("@Start", (pageNum - 1) * 9 + 1)
                                                        , new MySqlParameter("@End", pageNum * 9));
            }
            else
            {
                products = MySqlHelper.ExecuteDataTable(@"select * from T_Product order by Id limit @Start, @End",
                                                        new MySqlParameter("@Start", (pageNum - 1) * 9 + 1)
                                                        , new MySqlParameter("@End", pageNum * 9));
            }

            long totalCount;

            if (hasCategoryId)
            {
                int categoryId = Convert.ToInt32(context.Request["CategoryId"]);
                totalCount = (long)MySqlHelper.ExecuteScalar("select count(*) from T_Product where CategoriyId=@CategoryId", new MySqlParameter("@CategoryId", categoryId));
            }
            else
            {
                totalCount = (long)MySqlHelper.ExecuteScalar("select count(*) from T_Product");
            }
            int pageCount = (int)Math.Ceiling(totalCount / 9.0);

            object[] pageData = new object[pageCount];
            for (int i = 0; i < pageCount; i++)
            {
                pageData[i] = new { Href = "ProductList.ashx?PageNum=" + (i + 1), Title = i + 1 };
            }
            var data = new
            {
                Products          = products.Rows,
                PageData          = pageData,
                TotalCount        = totalCount,
                PageNum           = pageNum,
                PageCount         = pageCount,
                ProductCategories = MySqlHelper.ExecuteDataTable(@" select * from 
                    T_ProductCategories ").Rows
            };
            string html = CommonHelper.RenderHtml("Front/ProductList.html", data);

            context.Response.Write(html);
        }
Пример #4
0
 public static DataRowCollection GetProductCategories()
 {
     return(MySqlHelper.ExecuteDataTable(@" select * from 
             T_ProductCategories ").Rows);
 }
Пример #5
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["Action"];
            string status;

            if ("PostComment".Equals(action))
            {
                int    productId = Convert.ToInt32(context.Request["ProductId"]);
                string title     = context.Request["Title"];
                string msg       = context.Request["Msg"];
                if (productId == 0 || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(msg))
                {
                    status = "数据不能为空";
                }
                else
                {
                    int line = MySqlHelper.ExecuteNonQuery(@"insert into T_ProductComments (ProductId
                    ,Title,Msg,CreateDateTime) values (@ProductId,@Title,@Msg,@CreateDateTime)"
                                                           , new MySqlParameter("@ProductId", productId)
                                                           , new MySqlParameter("@Title", title)
                                                           , new MySqlParameter("@Msg", msg)
                                                           , new MySqlParameter("@CreateDateTime", DateTime.Now.ToLocalTime().ToString()));
                    if (line == 1)
                    {
                        status = "OK";
                    }
                    else
                    {
                        status = "插入数据失败!";
                    }
                }
                context.Response.Write(status);
            }
            else if ("Load".Equals(action))
            {
                int       productId = Convert.ToInt32(context.Request["ProductId"]);
                DataTable dt        = MySqlHelper.ExecuteDataTable(@"select * from T_ProductComments
                where ProductId=@ProductId", new MySqlParameter("@ProductId", productId));
                //一般不要把DataTable等对象直接通过Json传递给客户端,一般应该只传递基本类型或者
                //POCO(Plain Object C# Object 简单的类,只有属性的类)或是POCO的简单结合

                object[] comments = new object[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow  row      = dt.Rows[i];
                    DateTime createDT = (DateTime)row["CreateDateTime"];
                    comments[i] = new
                    {
                        Title          = row["Title"],
                        Msg            = row["Msg"],
                        CreateDateTime = createDT.ToString()
                    };
                }
                string json = new JavaScriptSerializer().Serialize(comments);
                context.Response.Write(json);
            }
            else
            {
            }
        }