sql连接/隔离存储错误c#控制台

flmtquvp  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(314)
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);

            }
        }

它总是指向catch{},所以一定有一些我已经检查过的错误,比如连接字符串。效果很好。错误在哪里
希望有人能帮我。。。
我使用了一段时间前制作的windows窗体应用程序中的代码。这门课应该学得很好,但不幸的是没有。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题