码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-04-04 13:04:07      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

一。删除

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

二。插入

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# 从数据库中删除,插入,修改 索引选中条目

标签:

原文地址:http://www.cnblogs.com/wenjieyatou/p/5351693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!