我现在正在编写 C# 函数,它必须返回我的用户密码。一切都很好,但是我试图编译这个项目,VS 告诉我未分配的变量。
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;
}
在这里我无法返回 resultPassword,因为变量未分配:|