protected void Page_Load(object sender, EventArgs e) { string xmlFilePath = Server.MapPath("~/Category.xml"); XDocument doc = new XDocument ( new XDeclaration("1.0", "utf-8", "yes"), new XComment("分类"), new XElement("Categories") ); doc.Save(xmlFilePath); XElement els = XElement.Load(xmlFilePath); MyPetShopDataContext db = new MyPetShopDataContext(); var results = from c in db.Category select c; foreach (Category category in results) { XElement el = new XElement("Category", new XElement("CategoryId", category.CategoryId), new XElement("Name", category.Name), new XElement("Descn", category.Descn)); els.Add(el); } els.Save(xmlFilePath); Response.Redirect("~/Category.xml"); }
protected void btnSearch_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); // 查找分类名中包含输入内容的分类 string searchMsg = txtSearch.Text.Trim(); var results = searchMsg != "" ? from c in db.Category where SqlMethods.Like(c.Name, "%" + searchMsg + "%") select c : null; if (results != null) { if (results.Count() != 0) { if (lbMsg.Text != null) { lbMsg.Text = ""; } gvCategory.DataSource = results; gvCategory.DataBind(); } else { lbMsg.Text = "没有满足条件的数据!"; } } else { lbMsg.Text = "不能为空!"; } }
protected void BtnAll_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); //定义MyPetShopDataContext类实例db var products = from r in db.Product //db是公共的MyPetShopDataContext类实例 select r; gvProduct.DataSource = products; //将LINQ查询结果设置为gvProduct的数据源 gvProduct.DataBind(); //显示数据源中的数据 }
protected void Page_Load(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Category select r; GridView1.DataSource = results; GridView1.DataBind(); }
protected void Button1_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var products = from r in db.Product select r; GridView1.DataSource = products; GridView1.DataBind(); }
protected void btnFuzzy_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product where System.Data.Linq.SqlClient.SqlMethods.Like(r.Name, "%c%") && r.ListPrice > 30 select r; gvProduct.DataSource = results; gvProduct.DataBind(); }
protected void BtnOrder_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Products orderby r.UnitCost descending select r; gvProduct.DataSource = results; gvProduct.DataBind(); }
protected void BtnSelect_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Products where r.UnitCost > 20 select r; gvProduct.DataSource = results; gvProduct.DataBind(); }
protected void btnInsert_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); Category category = new Category(); category.Name = txtName.Text; category.Descn = txtDescn.Text; db.Category.InsertOnSubmit(category); db.SubmitChanges(); }
protected void Button4_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product orderby r.UnitCost descending select r; GridView1.DataSource = results; GridView1.DataBind(); }
protected void Button9_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product where System.Data.Linq.SqlClient.SqlMethods.Like(r.Name, "%fly%") select r; GridView1.DataSource = results; GridView1.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product where r.CategoryId == int.Parse(Request.QueryString["CategoryId"]) select r; GridView1.DataSource = results; GridView1.DataBind(); }
/// <summary> /// 显示登录的用户名对应的订单列表 /// </summary> protected void Bind() { MyPetShopDataContext db = new MyPetShopDataContext(); var orderItems = from o in db.OrderItem where o.Order.UserName == User.Identity.Name select o; gvOrderItem.DataSource = orderItems; gvOrderItem.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var products = (from p in db.Product orderby p.ProductId descending select p).Take(7); gvProduct.DataSource = products; gvProduct.DataBind(); }
protected void Button1_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); Category category = new Category(); category.Name = TextBox1.Text; category.Descn = TextBox2.Text; db.Category.InsertOnSubmit(category); db.SubmitChanges(); GridView1.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Categories select r; gvCategory.DataSource = results; gvCategory.DataBind(); } }
protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e) { lbl_Count.Text = ddl_Category.SelectedValue; MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product where r.CategoryId == int.Parse(ddl_Category.SelectedValue) select r; gv_Category.DataSource = results; gv_Category.DataBind(); }
protected void Button1_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var category = (from r in db.Category where r.CategoryId == int.Parse(TextBox1.Text) select r).First(); category.Name = TextBox2.Text; category.Descn = TextBox3.Text; db.SubmitChanges(); Response.Redirect("~/GridUpdate.aspx"); }
protected void BtnUpdate_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var category = (from r in db.Category where r.CategoryId == int.Parse(txtCategoryId.Text) select r).First(); category.Name = txtName.Text; category.Descn = txtDescn.Text; db.SubmitChanges(); Response.Redirect("~/Chap8/GridUpdate.aspx"); }
protected void Button3_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product select new { r.ProductId, r.CategoryId }; gvProduct3.DataSource = results; gvProduct3.DataBind(); }
protected void DetailsView1_ItemDeleting(Object sender, DetailsViewDeleteEventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var productCount = (from p in db.Product where p.SuppId == int.Parse(DetailsView1.DataKey.Value.ToString()) select p).Count(); if (productCount != 0) { lblError.Text = "Error:该分类下有商品,请先删除商品!"; e.Cancel = true; } }
protected void BtnProject_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); //定义MyPetShopDataContext类实例db var products = from r in db.Product //db是公共的MyPetShopDataContext类实例 select new { r.ProductId, r.CategoryId, r.Name }; gvProduct.DataSource = products; gvProduct.DataBind(); }
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e) { if (ddlCategory.SelectedValue == "0") { return; } MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Product where r.CategoryId == int.Parse(ddlCategory.SelectedValue) select r; gvProduct.DataSource = results; gvProduct.DataBind(); }
protected void BtnQuote_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from r in db.Products select new { r.ProductId, r.CategoryId, CategoryName = r.Category.Name }; gvProduct.DataSource = results; gvProduct.DataBind(); }
protected void btnSubmit_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); //在Order表中添加记录 Order order = new Order(); order.UserName = User.Identity.Name; order.OrderDate = DateTime.Now; order.Addr1 = txtAddr1.Text.Trim(); order.Addr2 = txtAddr2.Text.Trim(); order.City = txtCity.Text.Trim(); order.State = txtState.Text.Trim(); order.Zip = txtZip.Text.Trim(); order.Phone = txtPhone.Text.Trim(); order.Status = "未审核"; db.Order.InsertOnSubmit(order); db.SubmitChanges(); //在OrderItem中添加记录 int orderId = order.OrderId; for (int i = 0; i < Profile.Cart.ProName.Count; i++) { OrderItem orderItem = new OrderItem(); orderItem.OrderId = orderId; orderItem.ProName = (string)Profile.Cart.ProName[i]; orderItem.ListPrice = (decimal)Profile.Cart.ListPrice[i]; orderItem.Qty = (int)Profile.Cart.Qty[i]; orderItem.TotalPrice = (int)Profile.Cart.Qty[i] * (decimal)Profile.Cart.ListPrice[i]; //修改Product表的商品库存 var product = (from p in db.Product where p.ProductId == (int)Profile.Cart.ProId[i] select p).First(); product.Qty -= orderItem.Qty; db.OrderItem.InsertOnSubmit(orderItem); db.SubmitChanges(); } //清空各数组列表对象 Profile.Cart.Qty.Clear(); Profile.Cart.ProName.Clear(); Profile.Cart.ProId.Clear(); Profile.Cart.ListPrice.Clear(); Profile.Cart.TotalPrice = ""; pnlConsignee.Visible = false; lblMsg.Text = "已经成功结算,谢谢光临!"; }
protected void BtnJoin_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from product in db.Products join category in db.Categories on product.CategoryId equals category.CategoryId select new { product.ProductId, product.CategoryId, CategoryName = category.Name }; gvProduct.DataSource = results; gvProduct.DataBind(); }
protected void btnSearch_Click(object sender, EventArgs e) { MyPetShopDataContext db = new MyPetShopDataContext(); var results = from c in db.Category where System.Data.Linq.SqlClient.SqlMethods.Like(c.Name, "%" + txtSearch.Text + "%") select c; if (results.Count() != 0) { gvCategory.DataSource = results; gvCategory.DataBind(); } else { lblMsg.Text = "没有满足条件的数据!"; } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack & Request.QueryString["CategoryId"] != null) { int categoryid = int.Parse(Request.QueryString["CategoryId"]); MyPetShopDataContext db = new MyPetShopDataContext(); var category = (from r in db.Category where r.CategoryId == categoryid select r).First(); TextBox1.ReadOnly = true; TextBox1.Text = category.CategoryId.ToString(); TextBox2.Text = category.Name; TextBox3.Text = category.Descn; } }
public string[] GetStrings(string prefixText, int count) { MyPetShopDataContext db = new MyPetShopDataContext(); //模糊查找与关连文本框中输入值匹配的商品 var products = from p in db.Product where SqlMethods.Like(p.Name, "%" + prefixText.Trim() + "%") select p; //将查找到商品的商品名填充到列表类中 List <String> list = new List <String>(count); foreach (var product in products) { list.Add(product.Name); } return(list.ToArray()); }
/// <summary> /// 根据从OrderMaster.aspx传递过来的OrderId值,显示与OrderId值相等的订单记录和订单详细记录 /// </summary> protected void Bind() { MyPetShopDataContext db = new MyPetShopDataContext(); int orderId = int.Parse(Request.QueryString["OrderId"]); var order = from o in db.Order where o.OrderId == orderId select o; var orderItem = from i in db.OrderItem where i.OrderId == orderId select i; gvOrder.DataSource = order; gvOrder.DataBind(); gvOrderItem.DataSource = orderItem; gvOrderItem.DataBind(); }