Exemple #1
0
        private void deleteDataFromTable(object sender, RoutedEventArgs e)
        {
            string sql    = "delete from material where ID =" + deleteID.Text;
            bool   result = adb.deleteFromTableData(sql, Int32.Parse(deleteID.Text));

            if (!result)
            {
                MessageBox.Show("删除失败!", "警告");
            }
        }
        // 删除该条ListBoxItem,并从数据库中删除
        private void deleteMaterialInDatabase(object sender, RoutedEventArgs e)
        {
            if (currentClickRowOfListBox == -1)
            {
                return;
            }
            ListBoxItem currentClickMaterialNameItem = (ListBoxItem)(listBox_materialName.Items[currentClickRowOfListBox]);
            ListBoxItem currentClickMatNameItem      = (ListBoxItem)(listBox_matName.Items[currentClickRowOfListBox]);
            ListBoxItem currentClickSoeNameItem      = (ListBoxItem)(listBox_soeName.Items[currentClickRowOfListBox]);
            // 删除
            //从数据库中删除
            //维持findFromDataBaseMaterials不变,只是统计删除ListBoxItem的tag值,最后再调用接口删除
            int index = Convert.ToInt32(currentClickMaterialNameItem.Tag);

            //findFromDataBaseMaterials.RemoveAt(index);
            if (!deleteMaterialIndexInfindFromDataBaseMaterials.Contains(index))
            {
                deleteMaterialIndexInfindFromDataBaseMaterials.Add(index);
            }
            // 最终从数据库中删除哪些material从findFromDataBaseMaterials和deleteMaterialIndexInfindFromDataBaseMaterials得出
            // 在关闭本窗口前从数据库中删除!!!

            //从数据库中删除数据
            for (int i = 0; i < deleteMaterialIndexInfindFromDataBaseMaterials.Count; i++)
            {
                //要从三个表中删除相应数据
                //根据material表获取另外两个表的id
                int temp = deleteMaterialIndexInfindFromDataBaseMaterials.ElementAt(i);
                Dictionary <string, string> mat = ((Dictionary <string, string>)((Dictionary <string, Dictionary <string, string> >)findFromDataBaseMaterials[temp])["mat"]);
                string matId = mat["MID"];
                Dictionary <string, string> eos = ((Dictionary <string, string>)((Dictionary <string, Dictionary <string, string> >)findFromDataBaseMaterials[temp])["soe"]);
                string eosId        = eos["EOSID"];
                string materialName = ((Dictionary <string, string>)((Dictionary <string, Dictionary <string, string> >)findFromDataBaseMaterials[temp])["materialName"])["content"];
                string matName      = ((Dictionary <string, string>)((Dictionary <string, Dictionary <string, string> >)findFromDataBaseMaterials[temp])["matName"])["content"];
                string eosName      = ((Dictionary <string, string>)((Dictionary <string, Dictionary <string, string> >)findFromDataBaseMaterials[temp])["soeName"])["content"];

                string sql     = "delete from Material where material_name = '" + materialName + "'";
                bool   success = adb.deleteFromTableData(sql, 0);
                if (success)
                {
                    Console.WriteLine("从material表中删除成功");
                }
                else
                {
                    Console.WriteLine("从material表中删除失败");
                }

                string matTableName = "Mat_" + matName;
                success = adb.deleteFromTableData("delete from " + matTableName + " where MID = '" + matId + "'", 0);
                if (success)
                {
                    Console.WriteLine("从mat表中删除成功");
                }
                else
                {
                    Console.WriteLine("从mat表中删除失败");
                }

                string eosTableName = "Eos_" + eosName;
                success = adb.deleteFromTableData("delete from " + eosTableName + " where EOSID = '" + eosId + "'", 0);
                if (success)
                {
                    Console.WriteLine("从eos表中删除成功");
                }
                else
                {
                    Console.WriteLine("从eos表中删除失败");
                }
            }

            // end
            // 从ListBox中删除
            listBox_materialName.Items.Remove(currentClickMaterialNameItem);
            listBox_matName.Items.Remove(currentClickMatNameItem);
            listBox_soeName.Items.Remove(currentClickSoeNameItem);
        }