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;
}
C# MySql数据库
最新推荐文章于 2024-08-22 10:10:35 发布