• 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();
                }
            }
  • 相关阅读:
    梯度下降进阶
    梯度下降基础
    python---matplotlib
    python---numpy
    浅析Jupyter Notebook
    anaconda安装
    机器学习---导学
    python---线程与进程
    mapping values are not allowed in this context at line 115 column 10
    laravel进行单元测试的时候如何模拟数据库以及mockery的调用
  • 原文地址:https://www.cnblogs.com/wenjieyatou/p/5351693.html
Copyright © 2020-2023  润新知