• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一。删除

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap