make function if c# argument is admin, run sql command to all Region% like dbs
ex: helper.exe admin

INSERT INTO dbo.Users
(
    UserName, UserID, Password, UserType, FirstName, LastName, Access, HashCode
)
VALUES
(   'Admin2', 
    '0',  
    '85c84b2ad28096bf3b6e01ff6f716eac9c50c1a1e2471f990c8b6000d47ec6ff',
    1,   
    'Admin',   
    '0',
    'Report,StandardRpg,ListPayRpg,LoanRpg,DarmaniRpg,BazRpg,TakmiliRpg,TaminRpg,ExportTamin,BankRpg,IncrRpg,IncrDaraeeRpg,SendDaraeeRpg,MissionRpg,TaxRpg,OverPayRpg,OldRpg,DosRpg,ConvertWinDos,ConvertPublic,Facility,ViewEntity,RuleEntity,CodePay,InsBaseType,RuleIncludeCodeType', 
    '-192243656'
)



c# code below:




using System;
using System.Data.SqlClient;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string configFilePath = "config.txt";
        string connectionString;

        // Check if config.txt exists and read the server information
        if (File.Exists(configFilePath))
        {
            string[] configLines = File.ReadAllLines(configFilePath);
            string server = "";

            foreach (var line in configLines)
            {
                if (line.StartsWith("server="))
                {
                    server = line.Split('=')[1].Trim();
                    break;
                }
            }

            if (string.IsNullOrEmpty(server))
            {
                Console.WriteLine("Server information is missing in the config file.");
                return;
            }

            //connectionString = $"Server={server};Integrated Security=True;";
	    connectionString = string.Format("Server={0};Integrated Security=True;", server);
        }
        else
        {
            //Console.WriteLine($"Config file '{configFilePath}' does not exist.");
            Console.WriteLine("Config file '" + configFilePath + "' does not exist.");
	    return;
        }

        string loginName = "Salary";
        string password = "salary";

        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                // Create Login if it does not exist
                CreateLogin(connection, loginName, password);

                // Get all databases that start with "Region"
                var databases = GetRegionDatabases(connection);

                foreach (var dbName in databases)
                {
                    CreateUserAndAssignRoles(connection, dbName, loginName);
                }

                Console.WriteLine("User creation and role assignment completed successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }

    private static void CreateLogin(SqlConnection connection, string loginName, string password)
    {
        string createLoginSql =
            "IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = @LoginName) " +
            "BEGIN " +
            "DECLARE @SQL NVARCHAR(MAX); " +
            "SET @SQL = 'CREATE LOGIN [' + @LoginName + '] WITH PASSWORD = ''' + @Password + ''';'; " +
            "EXEC sp_executesql @SQL; " +
            "ALTER SERVER ROLE [bulkadmin] ADD MEMBER [" + loginName + "]; " +
            "ALTER SERVER ROLE [diskadmin] ADD MEMBER [" + loginName + "]; " +
            "END";

        using (SqlCommand command = new SqlCommand(createLoginSql, connection))
        {
            command.Parameters.AddWithValue("@LoginName", loginName);
            command.Parameters.AddWithValue("@Password", password);
            command.ExecuteNonQuery();
        }
    }

    private static string[] GetRegionDatabases(SqlConnection connection)
    {
        string query = "SELECT name FROM sys.databases WHERE name LIKE 'Region%'";
        using (SqlCommand command = new SqlCommand(query, connection))
        using (SqlDataReader reader = command.ExecuteReader())
        {
            var databases = new System.Collections.Generic.List<string>();
            while (reader.Read())
            {
                databases.Add(reader.GetString(0));
            }
            return databases.ToArray();
        }
    }

    private static void CreateUserAndAssignRoles(SqlConnection connection, string dbName, string loginName)
    {
        Console.WriteLine("Current Database is: " + dbName);

        // Use a new SqlConnection for the specific database context
        using (SqlConnection dbConnection = new SqlConnection(connection.ConnectionString + ";Database=" + dbName))
        {
            dbConnection.Open();

            // Check if the user exists and drop if it does
            string dropUserSql =
                "IF EXISTS (SELECT * FROM sys.database_principals WHERE name = @LoginName) " +
                "BEGIN " +
                "DROP USER [" + loginName + "]; " +
                "END";

            using (SqlCommand command = new SqlCommand(dropUserSql, dbConnection))
            {
                command.Parameters.AddWithValue("@LoginName", loginName);
                command.ExecuteNonQuery();
            }

            // Create user and assign roles
            string createUserSql =
                "CREATE USER [" + loginName + "] FOR LOGIN [" + loginName + "]; " +
                "ALTER USER [" + loginName + "] WITH DEFAULT_SCHEMA=[dbo]; " +
                "ALTER ROLE [db_accessadmin] ADD MEMBER [" + loginName + "]; " +
                "ALTER ROLE [db_datareader] ADD MEMBER [" + loginName + "]; " +
                "ALTER ROLE [db_datawriter] ADD MEMBER [" + loginName + "]; " +
                "ALTER ROLE [db_ddladmin] ADD MEMBER [" + loginName + "]; " +
                "ALTER ROLE [db_owner] ADD MEMBER [" + loginName + "];";

            using (SqlCommand command = new SqlCommand(createUserSql, dbConnection))
            {
                command.ExecuteNonQuery();
            }
        }
    }
}