当前位置:Gxlcms > 数据库问题 > C# 从数据库中删除,插入,修改 索引选中条目

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

(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;
                    }
                }
            }
        }

二。插入

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 = "提示:密码修改失败!";
                }

 

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

标签:

人气教程排行