Пример #1
0
        private void AddEquip()
        {
            //string "Key (size)=(1337) already exists."ж
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        Console.WriteLine("Enter the inventory type.");
                        inputStr = Console.ReadLine();
                        var query  = session.CreateQuery("from Invent_info as invent where invent.Invent_type = \'" + inputStr + '\'');
                        var invent = (Invent_info)query.UniqueResult();
                        if (invent == null)
                        {
                            Console.WriteLine("Adding new inventory type.");
                            invent = new Invent_info {
                                Invent_type = inputStr
                            };
                            session.SaveOrUpdate(invent);
                        }

                        Console.WriteLine("Enter the size.");
                        inputNum = GetUserOption();
                        var size = (Size_info)session.CreateQuery("from Size_info as size where size.Size = " + inputNum).UniqueResult();
                        if (size == null)
                        {
                            Console.WriteLine("Adding new size.");
                            size = new Size_info {
                                Size = inputNum
                            };
                            session.SaveOrUpdate(invent);
                        }

                        var completeKit = new Complete_info {
                            Invent = invent, Size = size
                        };
                        session.SaveOrUpdate(invent);
                        session.SaveOrUpdate(size);
                        session.SaveOrUpdate(completeKit);
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        //e.InnerException.Message.Contains("key");
                        Console.WriteLine("smth went wrong");
                    }
                }
            }
        }
Пример #2
0
        private void AlterEquip()
        {
            PrintEquip();
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        Console.WriteLine("Enter the kit id to alter.");
                        inputNum = GetUserOption();
                        var query = session.CreateQuery("from Complete_info as kit where kit.Id = " + inputNum);
                        var kit   = (Complete_info)query.UniqueResult();

                        if (kit == null)
                        {
                            Console.WriteLine("No such inventory kit.");
                            return;
                        }

                        Console.WriteLine("Enter the inventory type.");
                        inputStr = Console.ReadLine();
                        query    = session.CreateQuery("from Invent_info as invent where invent.Invent_type = \'" + inputStr + '\'');
                        var invent = (Invent_info)query.UniqueResult();
                        if (invent == null)
                        {
                            Console.WriteLine("Adding new inventory type.");
                            invent = new Invent_info {
                                Invent_type = inputStr
                            };
                            session.SaveOrUpdate(invent);
                        }

                        Console.WriteLine("Enter the size.");
                        inputNum = GetUserOption();
                        var size = (Size_info)session.CreateQuery("from Size_info as size where size.Size = " + inputNum).UniqueResult();
                        if (size == null)
                        {
                            Console.WriteLine("Adding new size.");
                            size = new Size_info {
                                Size = inputNum
                            };
                            session.SaveOrUpdate(invent);
                        }

                        kit.Invent = invent;
                        kit.Size   = size;
                        session.SaveOrUpdate(invent);
                        session.SaveOrUpdate(size);
                        session.SaveOrUpdate(kit);
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        //e.InnerException.Message.Contains("key");
                        Console.WriteLine("smth went wrong");
                    }
                }
            }
        }