private void ImportOrder_Click(object sender, EventArgs e) { try { using (var db = new OrderDB()) { OrderService os = new OrderService(); XmlSerializer xml = new XmlSerializer(typeof(List <OrderList>)); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Open the file"; openFileDialog.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*"; openFileDialog.AddExtension = true; openFileDialog.CheckFileExists = true; openFileDialog.CheckPathExists = true; openFileDialog.DefaultExt = "*.txt|*.txt"; if (openFileDialog.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open); //os.ListOfOrder = (List<Order>)xml.Deserialize(fs); foreach (OrderList order in (List <OrderList>)xml.Deserialize(fs)) { os.Add(order); } fs.Close(); IQueryable <OrderList> orders = from t in db.Order select t; var list = orders.ToList(); b.DataSource = new BindingList <OrderList>(list); OrderbindingSource.DataSource = b; } } }catch (Exception) { return; } }
public List <OrderList> QueryByClientName(string name) { using (var db = new OrderDB()) { return(db.Order.Include("ListOfDetails") .Where(o => o.ClientName.Equals(name)).ToList()); } }
public List <OrderList> QueryByOrderId(string id) { using (var db = new OrderDB()) { return(db.Order.Include("ListOfDetails") .Where(o => o.OrderId.Equals(id)).ToList()); } }
public OrderList GetOrder(string Id) { using (var db = new OrderDB()) { return(db.Order.Include("ListOfDetails"). SingleOrDefault(o => o.OrderId == Id)); } }
public void Add(OrderList order) { using (var db = new OrderDB()) { db.Order.Add(order); db.SaveChanges(); } }
public List <OrderList> QueryByTotalPriceOver(long num) { using (var db = new OrderDB()) { return(db.Order.Include("ListOfDetails") .Where(o => o.TotalPrice >= num).ToList()); } }
public List <OrderList> QueryByProductName(string name) { using (var db = new OrderDB()) { var query = db.Order.Include("ListOfDetails") .Where(o => o.ListOfDetails.Where( detail => detail.ProductName.Equals(name)).Count() > 0); return(query.ToList()); } }
public void Update(OrderList order) { using (var db = new OrderDB()) { db.Order.Attach(order); db.Entry(order).State = EntityState.Modified; order.ListOfDetails.ForEach(detail => db.Entry(detail).State = EntityState.Modified); db.SaveChanges(); } }
public void Delete(string orderId) { using (var db = new OrderDB()) { var order = db.Order.Include("ListOfDetails").SingleOrDefault(o => o.OrderId == orderId); //db.DetailsList.RemoveRange(order.ListOfDetails); db.Order.Remove(order); db.SaveChanges(); } }
public void Export(string xmlFileName) { using (var db = new OrderDB()) { XmlSerializer xmlser = new XmlSerializer(typeof(List <OrderList>)); FileStream fs = new FileStream(xmlFileName, FileMode.OpenOrCreate); xmlser.Serialize(fs, db.Order); fs.Close(); } }
private void MenuTool3_Click(object sender, EventArgs e) { //显示所有订单 using (var db = new OrderDB()) { IQueryable <OrderList> orders = from t in db.Order select t; var list = orders.ToList(); b.DataSource = new BindingList <OrderList>(list); OrderGridView.DataSource = b; } }
private void button1_Click(object sender, EventArgs e) { using (var db = new OrderDB()) { List <OrderList> list = new List <OrderList>(); OrderService os = new OrderService(); IQueryable <OrderList> orders = from t in db.Order select t; list = orders.ToList(); b.DataSource = new BindingList <OrderList>(list); if (textBox1.Text != "") { string s = textBox1.Text; if (comboBox2.Text == "订单号") { list = os.QueryByOrderId(textBox1.Text); } else if (comboBox2.Text == "用户名") { list = os.QueryByClientName(textBox1.Text); } else if (comboBox2.Text == "总价高于所输入值的") { list = os.QueryByTotalPriceOver(long.Parse(textBox1.Text)); } else { list = os.QueryByProductName(textBox1.Text); } if (comboBox1.Text == "查询订单") { if (CheckListNull(list) == true) { OrderbindingSource.DataSource = list; } } else { if (CheckListNull(list) == true) { foreach (OrderList o in list) { os.Delete(o.OrderId); } orders = from t in db.Order select t; list = orders.ToList(); OrderbindingSource.DataSource = b; } } } } }
private void ExportOrder_Click(object sender, EventArgs e) { try { using (var db = new OrderDB()) { XmlSerializer xmlser = new XmlSerializer(typeof(List <OrderList>)); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "Save the File"; saveFileDialog.Filter = "txt file(*.txt)|*.txt|html file(*.html)|*.html"; saveFileDialog.AddExtension = true; saveFileDialog.CheckPathExists = true; saveFileDialog.OverwritePrompt = true; saveFileDialog.DefaultExt = "*.txt|*.txt"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { //不论结果需要txt还是html,事先生成一个xml文件(txt) string filepath = saveFileDialog.FileName; filepath = Path.GetDirectoryName(filepath); string filename = Path.GetFileNameWithoutExtension(saveFileDialog.FileName); FileStream fs = new FileStream(filepath + filename + ".txt", FileMode.OpenOrCreate); xmlser.Serialize(fs, db.Order); fs.Close(); if (saveFileDialog.FilterIndex == 2) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(filepath + filename + ".txt"); XPathNavigator navigator = xmlDocument.CreateNavigator(); navigator.MoveToRoot(); XslCompiledTransform xslCompiledTransform = new XslCompiledTransform(); xslCompiledTransform.Load(@"..\..\OrderList.xslt"); FileStream fileStream = File.OpenWrite(saveFileDialog.FileName); XmlWriterSettings settings = new XmlWriterSettings(); XmlWriter xmlWriter = XmlWriter.Create(fileStream, settings); xslCompiledTransform.Transform(navigator, null, xmlWriter); File.Delete(filepath + filename + ".txt"); fileStream.Close(); } } } }catch (Exception) { MessageBox.Show("您所输入的订单有误!", "输入错误!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } }
private bool CheckListNull(List <OrderList> list) { if (list == null) { using (var db = new OrderDB()) { MessageBox.Show("未找到相关订单。", "输入错误!", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox1.Text = null; OrderbindingSource.DataSource = db.Order; return(false); } } return(true); }
// 序列化 public void Import(string xmlFileName) { using (var db = new OrderDB()) { XmlSerializer xmlser = new XmlSerializer(typeof(List <OrderList>)); FileStream fs = new FileStream(xmlFileName, FileMode.Open); //db.OrderList = (List<Order>)xmlser.Deserialize(fs); foreach (OrderList order in (List <OrderList>)xmlser.Deserialize(fs)) { Add(order); } fs.Close(); } }
private void MenuTool1_Click(object sender, EventArgs e) { using (var db = new OrderDB()) { IQueryable <OrderList> order = from t in db.Order select t; var list = order.ToList(); AddOrderForm addOrderForm = new AddOrderForm(list); addOrderForm.TransfEvent += AddOrderForm_Add; addOrderForm.ShowDialog(); order = from t in db.Order select t; list = order.ToList(); b.DataSource = new BindingList <OrderList>(list); OrderbindingSource.DataSource = b; } }
private void 修改订单ToolStripMenuItem_Click(object sender, EventArgs e) { using (var db = new OrderDB()) { string value = OrderGridView.Rows[OrderGridView.CurrentRow.Index].Cells[0].Value.ToString(); OrderList order = os.QueryByOrderId(value)[0]; OrderList o = os.QueryByOrderId(value)[0]; ChangeOrderForm changeOrderForm = new ChangeOrderForm(o); changeOrderForm.ShowDialog(); os.Update(o); IQueryable <OrderList> orders = from t in db.Order select t; var list = orders.ToList(); b.DataSource = new BindingList <OrderList>(list); OrderbindingSource.DataSource = b; } }
public IEnumerable <Order> QueryOrderbyId(string id) { using (var db = new OrderDB()) { return(db.Order.Include("Items"). SingleOrDefault(o => o.Id == Id)); } var query = orderlist.Where(o => o.OrderId.Equals(id)); if (query.FirstOrDefault() == null) { Console.WriteLine("订单号不存在!"); return(null); } else { return(query); } }
public List <OrderList> GetAllOrders() { using (var db = new OrderDB()) return(db.Order.Include("ListOfDetails").ToList()); }