Консоль SQL Connection / IsolatedStorage Ошибка C # - PullRequest
0 голосов
/ 13 ноября 2018
internal class activiation
{
    const string connectionString = "SERVER = xxx; PORT = xxx; DATABASE = xxx; USER ID = xxx; PASSWORD = xxx; SslMode = none";
    private static bool Activated { get; set; }

    public static bool isActivated(string key)
    {
        using (MySqlConnection mySqlConn = new MySqlConnection(connectionString))
        {
            string checkForActivationQuery = "SELECT activated FROM esSerial WHERE serialKey =@key";
            MySqlCommand cmd = new MySqlCommand(checkForActivationQuery, mySqlConn);
            cmd.Parameters.AddWithValue("@key", key);
            mySqlConn.Open();
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            if (result > 0)
            {
                return true;
            }
            return false;
        }
    }

    public static void activateSoftware(string key)
    {
        if (!isActivated(key))
        {
            using (MySqlConnection mySqlConn = new MySqlConnection(connectionString))
            {
                string checkForKeyQuery = "SELECT COUNT(*) FROM esSerial WHERE serialKey =@key";
                MySqlCommand cmd = new MySqlCommand(checkForKeyQuery, mySqlConn);
                cmd.Parameters.AddWithValue("@key", key);
                mySqlConn.Open();
                int result = Convert.ToInt32(cmd.ExecuteScalar());

                if (result > 0)
                {
                    updateActivation(key);
                    using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
                    {
                        using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("settings.txt", FileMode.CreateNew, isolatedStorageFile))
                        {
                            using (StreamWriter sw = new System.IO.StreamWriter(isolatedStorageFileStream))
                            {
                                sw.WriteLine(key);
                            }
                        }
                    }
                    Activated = true;
                }
                else
                {
                    Console.ForegroundColor = Color.DarkRed;
                    Console.WriteLine("Your License-Key was not correct. You can buy one from https://discord.gg/gcZPmBQ (Discord)");
                    Activated = false;
                    Console.WriteLine("Console will close in 5 Seconds...");
                    Thread.Sleep(5000);
                    Environment.Exit(0);
                }
            }
        }
        else
        {
            Console.ForegroundColor = Color.Green;
            Console.WriteLine("Your software has already been activated.");
        }
    }

    private static void updateActivation(string key)
    {
        using (MySqlConnection mySqlConn = new MySqlConnection(connectionString))
        {
            string updateQuery = "UPDATE esSerial SET activated = 1 WHERE serialKey =@key";
            MySqlCommand cmd = new MySqlCommand(updateQuery, mySqlConn);
            cmd.Parameters.AddWithValue("@key", key);
            mySqlConn.Open();
            cmd.ExecuteNonQuery();
            Console.ForegroundColor = Color.Red;
            Console.WriteLine("Your software has been activated, please restart the programm");
        }
    }
}

Это мой класс Теперь возникает проблема при запуске моей программы (консольное приложение C #) И я запускаю этот код:

// Program activation

        Green();
        Console.WriteLine("Checking for License...");

        using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
        {
            try
            {
                using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("settings.txt", FileMode.Open, isolatedStorageFile))
                {
                    using (StreamReader sr = new StreamReader(isolatedStorageFileStream))
                    {
                        var activated = activiation.isActivated(sr.ReadLine());
                        if (!activated)
                        {
                            Console.ForegroundColor = Color.Red;
                            Console.WriteLine("Program not Activated yet. Please activate it with putting in a working License: ");
                            activiation.activateSoftware(Console.ReadLine());
                        }
                        else
                        {
                            Console.ForegroundColor = Color.Green;
                            Console.WriteLine("Found Licensed Program");
                        }
                    }
                }
            }
            catch
            {
                Console.ForegroundColor = Color.Red;
                Console.WriteLine("Error, can't connect to Database. Closing in 5 Seconds...");
                Thread.Sleep(5000);
                Environment.Exit(1);

            }
        }

Это всегда идет на улов {}, поэтому должна быть какая-то ошибка Я уже проверил, как строка подключения. Работает отлично. Где ошибка

Надеюсь, кто-нибудь сможет мне помочь ...

Я использовал этот код из приложения Windows Forms, которое я сделал некоторое время назад. Класс должен работать нормально, но, к сожалению, это не так.

...