Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello guys and ladies I want to create a code that will add all my local users from Items from listbox
public void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
while added local users are in
private void LoadLocalUsers()
to the Logins in Microsoft SQL Server Management Studio.

What I have tried:

I did something like this:
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();
                }
            }
Posted
Comments
CHill60 14-Dec-23 5:55am    
You have forgotten to ask a question or state a problem
Dave Kreskowiak 14-Dec-23 10:40am    
You've been around here long enough to know that if you don't spell out a problem you're having or ask a specific question, you're not going to get an answer.

The quality of the answers you get is directly dictated by the quality of the questions you ask.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900