Example #1
0
        public static void Edit(ListView lvData)            // изменение пользователя
        {
            foreach (ListViewItem selectedItem in lvData.SelectedItems)
            {
                var selectedLogin  = selectedItem.Tag;
                var formUserUpdate = new FormInsertUpdate(FormInsertUpdate.FormType.Update, (string)selectedLogin)
                {
                    Login            = selectedItem.SubItems[0].Text,
                    Password         = selectedItem.SubItems[1].Text,
                    Salt             = selectedItem.SubItems[2].Text,
                    RegistrationDate = DateTime.Parse(selectedItem.SubItems[3].Text),
                    UserAccessType   = (AccessType)Enum.Parse(typeof(AccessType), selectedItem.SubItems[4].Text)
                };
                if (formUserUpdate.ShowDialog() == DialogResult.OK)
                {
                    using (var sConn = new SqlConnection(sConnStr))
                    {
                        sConn.Open();
                        var sCommand = new SqlCommand
                        {
                            Connection  = sConn,
                            CommandText = @"UPDATE [dbo].[Users]
                                              SET [Login] = @NewLogin
                                                 ,[Password] = @Password
                                                 ,[Salt] = @Salt
                                                 ,[RegistrationDate] = @RegistrationDate
                                                 ,[AccessType] = @AccessType
                                            WHERE Login = @OldLogin"
                        };
                        sCommand.Parameters.AddWithValue("NewLogin", formUserUpdate.Login);
                        if (formUserUpdate.NewPassword)
                        {
                            var hashManaged = new HashManaged();
                            var password    = hashManaged.ComputeHash(formUserUpdate.Password + formUserUpdate.Salt);
                            sCommand.Parameters.AddWithValue("Password", password);
                            selectedItem.SubItems[1].Text = password;
                        }
                        else
                        {
                            sCommand.Parameters.AddWithValue("Password", formUserUpdate.Password);
                            selectedItem.SubItems[1].Text = formUserUpdate.Password;
                        }
                        sCommand.Parameters.AddWithValue("Salt", formUserUpdate.Salt);
                        sCommand.Parameters.AddWithValue("RegistrationDate", formUserUpdate.RegistrationDate);
                        sCommand.Parameters.AddWithValue("OldLogin", selectedLogin);
                        sCommand.Parameters.AddWithValue("AccessType", formUserUpdate.UserAccessType.ToString());
                        sCommand.ExecuteNonQuery();

                        selectedItem.SubItems[0].Text = formUserUpdate.Login;
                        selectedItem.SubItems[2].Text = formUserUpdate.Salt;
                        selectedItem.SubItems[3].Text = formUserUpdate.RegistrationDate.ToLongDateString();
                        selectedItem.SubItems[4].Text = formUserUpdate.UserAccessType.ToString();
                        selectedItem.Tag = formUserUpdate.Login;
                    }
                }
            }
            lvData.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            lvData.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }
Example #2
0
        public static void Insert(ListView lvData)          // вставка нового пользователя
        {
            var formUserInsert = new FormInsertUpdate(FormInsertUpdate.FormType.Insert, null)
            {
                UserAccessType = AccessType.None
            };

            if (formUserInsert.ShowDialog() == DialogResult.OK)
            {
                using (var sConn = new SqlConnection(sConnStr))
                {
                    sConn.Open();
                    var sCommand = new SqlCommand
                    {
                        Connection  = sConn,
                        CommandText = @"INSERT INTO [dbo].[Users]
                                               ([Login]
                                               ,[Password]
                                               ,[Salt]
                                               ,[RegistrationDate]
                                               ,[AccessType])
                                        OUTPUT inserted.Login
                                        VALUES
                                              (@Login
                                              ,@Password
                                              ,@Salt
                                              ,@RegistrationDate
                                              ,@AccessType)"
                    };
                    sCommand.Parameters.AddWithValue("Login", formUserInsert.Login);
                    var hashManaged = new HashManaged();
                    var password    = hashManaged.ComputeHash(formUserInsert.Password + formUserInsert.Salt);
                    sCommand.Parameters.AddWithValue("Password", password);
                    sCommand.Parameters.AddWithValue("Salt", formUserInsert.Salt);
                    sCommand.Parameters.AddWithValue("RegistrationDate", formUserInsert.RegistrationDate);
                    sCommand.Parameters.AddWithValue("AccessType", Enum.GetName(typeof(AccessType), formUserInsert.UserAccessType));
                    var login  = sCommand.ExecuteScalar();
                    var lvItem = new ListViewItem(new[]
                    {
                        formUserInsert.Login,
                        password,
                        formUserInsert.Salt,
                        formUserInsert.RegistrationDate.ToLongDateString(),
                        formUserInsert.UserAccessType.ToString()
                    });
                    lvItem.Tag = login;
                    lvData.Items.Add(lvItem);
                }
            }
            lvData.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            lvData.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }