public void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
private void LoadLocalUsers()
string connectionString = "Data Source=(local);Initial Catalog=master;Integrated Security=True"; // Użyj 'using' do zapewnienia poprawnego zarządzania zasobami using (SqlConnection connection = new SqlConnection(connectionString)) { try { connection.Open(); foreach (var userName in uniqueUserNames) { // Używanie parametrów w zapytaniach SQL string checkUserQuery = "SELECT COUNT(*) FROM sys.server_principals WHERE name = @UserName"; SqlCommand checkUserCommand = new SqlCommand(checkUserQuery, connection); checkUserCommand.Parameters.AddWithValue("@UserName", userName); int userCount = (int)checkUserCommand.ExecuteScalar(); if (userCount == 0) { // Dodawanie nowego użytkownika do bazy danych string addUserQuery = $"CREATE LOGIN [{userName}] FROM WINDOWS"; SqlCommand addUserCommand = new SqlCommand(addUserQuery, connection); addUserCommand.ExecuteNonQuery(); } // Dodawanie użytkownika do roli lub innej kategorii, jeśli to konieczne // Przykładowe zapytanie: // string addToRoleQuery = $"ALTER SERVER ROLE [localhost] ADD MEMBER [{userName}]"; // SqlCommand addToRoleCommand = new SqlCommand(addToRoleQuery, connection); // addToRoleCommand.ExecuteNonQuery(); } } catch (Exception ex) { // Obsługa błędów MessageBox.Show($"Wystąpił błąd: {ex.Message}", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Upewnij się, że połączenie jest zawsze zamykane, nawet jeśli wystąpił błąd connection.Close(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)