简体   繁体   中英

C# Set variable on MySQL query

i am writing C# function now, which must return me user password. All is fine, but than i am trying to compile this project, VS telling me about unassigned variable.

string sqlLoginCheck (string userId)
    {
        string database, host, user, pass, sqlParams, sqlQuery;
        string resultPassword;

        database = "db";
        host = "localhost";
        user = "root";
        pass = "....";

        sqlParams = "Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + pass;
        sqlQuery = "SELECT `id`, `name`, `pass` FROM `users` WHERE name = '" + userId + "' LIMIT 1";

        MySqlConnection sqlConnection = new MySqlConnection(sqlParams);
        MySqlCommand sqlCommand = new MySqlCommand(sqlQuery, sqlConnection);

        // Выполняем
        try
        {
            sqlConnection.Open();

            MySqlDataReader sqlReader = sqlCommand.ExecuteReader();

            if (sqlReader.HasRows)
            {
                while (sqlReader.Read())
                {
                    resultPassword = sqlReader[2].ToString();
                }
            }
            else
            {
                resultPassword = "";
            }
        }

        catch (MySqlException sqlError)
        {
            errorMessage.Text = "Ошибка: " + sqlError.Message;

            FileInfo errorLog = new FileInfo("errorLog.txt");

            if (errorLog.Exists == false)
            {
                FileStream errorFs = errorLog.Create();
                errorFs.Close();
            }

            StreamWriter errorSw = errorLog.AppendText();

            errorSw.WriteLine(DateTime.Now + " | " + sqlError.Message);
            errorSw.Close();
        }

        finally
        {
            sqlConnection.Close();
        }

        return resultPassword;

    }   

Here i can't return resultPassword, because variable is unassigned :|

Declare this variable as

string resultPassword = null;

Or you can declare it as

string resultPassword = String.Empty;

in that case you don't really need

else
{
            resultPassword = "";
}

because default value for password has already been set at declaration.

The resultPassword is declared in the function body, but it is initialized only inside the try block: so in case of exception, the variable will be unassigned.

Initialize the variable outside the try-catch block.

Some details here .

The compiler sees that there is a chance that resultPassword never gets assigned (if you go into the Catch or Finally. Just do :

string resultPassword = ""; 

in the declaration

It's because there is a root through the method where resultPassword is never assigned a value. As others have said declare:

string resultPassword = null;

I would suggest

string resultPassword = ""; 

then if you need to set to not found, actually set it to say "** not found **" or something. As it stands you have paths down your code where the variable isnt set - eg it enters the catch area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM