• C# 从数据库中删除,插入,修改 索引选中条目


    一。删除

    1.

                while (usrListView.SelectedIndex != -1)
                {
                    var currentSelectIndex = usrListView.SelectedIndex;
                    var item = usrView[currentSelectIndex];
                    using (StockManageDataContext smDataContext = new StockManageDataContext())
                    {
                        try
                        {
                            smDataContext.ExecuteCommand("DELETE FROM USERS WHERE ID={0}", item.ID);
                            usrView.RemoveAt(currentSelectIndex);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }

    2.

            private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
            {
    
                if (AddressList.SelectedIndex == -1)
                {
                    MessageBox.Show("请选择一项!");
                    return;
                }
                    if (MessageBox.Show("确认删除所选定的供应商信息?", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        while (AddressList.SelectedIndex != -1){
                        var currentSelectIndex = AddressList.SelectedIndex;
                        SHDZ selectedAddress = (SHDZ)AddressList.SelectedValue;
                        using (WJKCDataContext wjkc = new WJKCDataContext())
                        {
                            try
                            {
                                wjkc.ExecuteCommand("DELETE FROM SHDZ WHERE CODE={0}", selectedAddress.CODE);
                                addressView.RemoveAt(currentSelectIndex);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        return;
                        }
                    }
                }
            }

    3.

               var item = smDataContext.Suppliers.SingleOrDefault(c => c.ID == (int)hl.Tag);
                    if (item != null)
                    {
                        try2016-04-04
                        {
                            smDataContext.Suppliers.DeleteOnSubmit(item);
                            smDataContext.SubmitChanges();
                            MessageBox.Show("删除成功!");
                            GetSupplierData();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("删除时发生错误!错误信息:"+ex.Message);
                        }
                    }
                    smDataContext.Dispose();
                }

    二。插入

    1.

                    try
                    {
                        using (StockManageDataContext smDataContext = new StockManageDataContext())
                        {
                            smDataContext.Users.InsertOnSubmit(new User { UserName = usrName.Text, Password = pwd, Permission = permissionLevel });
                            smDataContext.SubmitChanges();
                            MessageBox.Show("添加用户改成功!");
                            usrView.Clear();
                            GetData();
                            usrName.IsEnabled = false;
                            PasswordBox1.IsEnabled = false;
                            PasswordBox2.IsEnabled = false;
                            PermimissionLevels.IsEnabled = false;
                            button1.IsEnabled = false;
                            usrName.Text = string.Empty;
                            PasswordBox1.Password = string.Empty;
                            PasswordBox2.Password = string.Empty;
                            PermimissionLevels.SelectedIndex = 0;
                        }
                    }

    2.

                    try
                    {
                        wjkc.ExecuteCommand(" insert into SHDZ (CODE,DZ,DETAIL,ISTY) values({0},{1},{2},{3})",
                         Code, txtAName.Text.Trim(), txtADetails.Text.Trim(), address.ISTY);
                        lableMsg.Content = "信息添加成功!";
                        lableMsg.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 255));
                        ClearControls();
                        winParent.Close();
                       // AddressInfoWindow win = new AddressInfoWindow();
                       // win.Show();
                        
                    }

    三  编辑

    1.

    确保 编辑的对象是所选对象,所以传入参数要注意,并且编辑时要先填充控件。

                    try
                    {
    
                        wjkc.ExecuteCommand("update SHDZ set DETAIL={0},DZ={1},ISTY={2} where CODE={3};"
                            , txtADetails.Text.Trim(), txtAName.Text.Trim(),address.ISTY,address.CODE);
                        lblMsg.Content = "信息修改成功!";
                        lblMsg.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 255));
                       // this.Window_Closed();
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);           
                    }

    2,。

                    try
                    {
                        using (StockManageDataContext smDataContext = new StockManageDataContext())
                        {
                            string usrname = (this.Parent as LoginWindow).User;
                            var s = smDataContext.Users.Single(c => c.UserName == usrname);
                            s.Password = pwd;
                            smDataContext.SubmitChanges();
                            MessageBox.Show("密码添加成功!");
                            this.NavigationService.Navigate(new Uri("LoginPage.xaml", UriKind.Relative));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        label_warning.Content = "提示:密码修改失败!";
                    }

     四,根据Index的值  来判断要编辑item还是要新增item。

    此步是获得index,前提是父窗口或者子窗口已经传入一个带参。

            public int Index { get; set; }
            public SupplierAM(int index)
            {
                Index = index;
                InitializeComponent();
                if (Index > 0)
                {
                    header.Content = "修改供应商信息";
                    StockManageDataContext smDataContext = new StockManageDataContext();
                    var item = smDataContext.Suppliers.SingleOrDefault(c => c.ID == Index);
                    textBox_name.Text = item.PsName;
                    textBox_contacter.Text = item.PsContacter;
                    textBox_tel.Text = item.PsTel;
                    textBox_fax.Text = item.PsFax;
                }
                else
                    header.Content = "添加供应商信息";
            }

    此步是提交按钮的事件。

           private void button_submit_Click(object sender, RoutedEventArgs e)
            {
                StockManageDataContext smDataContext = new StockManageDataContext();
                if (Index > 0)
                {
                    var item = smDataContext.Suppliers.SingleOrDefault(c => c.ID == Index);
                    item.PsName = textBox_name.Text;
                    item.PsContacter = textBox_contacter.Text;
                    item.PsTel = textBox_tel.Text;
                    item.PsFax = textBox_fax.Text;
                }
                else
                {
                    Supplier item = new Supplier();
                    item.PsName = textBox_name.Text;
                    item.PsContacter = textBox_contacter.Text;
                    item.PsTel = textBox_tel.Text;
                    item.PsFax = textBox_fax.Text;
                    smDataContext.Suppliers.InsertOnSubmit(item);
                }
                try
                {
                    smDataContext.SubmitChanges();
                    MessageBox.Show("操作成功!");
                    DialogResult = true;
                    Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发生错误!错误信息:" + ex.Message);
                }
                finally
                {
                    smDataContext.Dispose();
                }
            }
  • 相关阅读:
    Qt编程之右键单击QTreeWidgetItem弹出菜单
    Qt编程之QString 处理换行
    配置zbar识别二维码(转载)
    我学习图像处理的小结
    自己动手,实现“你的名字”滤镜
    结合grabcut和inpaint,实现人像去除
    (转载)找圆算法((HoughCircles)总结与优化
    寻找白板上的便签条
    寻找精密光学标定板上的矩形(网友提问)
    OpenCV中Denoising相关函数的简单介绍
  • 原文地址:https://www.cnblogs.com/wenjieyatou/p/5351693.html
Copyright © 2020-2023  润新知