MySQL 连接器 C#,MySQLDataRead.Read() 仅 运行 返回值时
MySQL Connector C#, MySQLDataRead.Read() only running when a value returned
顾名思义,这是 Reader 的 while 循环仅在 运行 返回值时出现的问题。在这种情况下见下文,在 if (email == rdr[0].ToString())
returns true 的情况下,while 循环本身会执行。但是,如果它不是 true,则 else 不会执行。同样重要的是要注意, Debug.Log("Checking to see if the account exists");
只有在帐户存在的情况下才会实际执行,正如支票所希望的那样。
我的假设是,如果连接器找不到一行,它不会 运行 循环。我怎样才能实现这个 运行ning?
谢谢。
conn.Open();
Debug.Log("SUCCESSFULL CONNECTION!");
if (!isLogin)
{
Debug.Log("Is not login");
bool doesEmailExist = false;
string seecQuery = "SELECT * FROM accounts WHERE email = '"+ email +"'";
Debug.Log(seecQuery);
MySqlCommand cmd = new MySqlCommand(seecQuery, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Debug.Log("Checking to see if the account exists");
if (email == rdr[0].ToString())
{
Debug.Log("The account exists");
accountExists.SetActive(true);
//doesEmailExist = true;
}
else //if (!doesEmailExist)
{
Debug.Log("Email is not in use. Starting OTA");
StartCoroutine(sendOTA()); // Start the OTP process
OTARead.SetActive(true);
}
这只是一个更新,我设法通过解决方法解决了这个问题。
这里是。我没有执行 while 循环,而是检查 Reader 是否返回了任何行。工作正常。
conn.Open();
Debug.Log("SUCCESSFULL CONNECTION!");
if (!isLogin)
{
Debug.Log("Is not login");
bool doesEmailExist = false;
string seecQuery = "SELECT * FROM accounts WHERE email = '"+ email +"'";
Debug.Log(seecQuery);
MySqlCommand cmd = new MySqlCommand(seecQuery, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
if (!rdr.HasRows)
{
Debug.Log("Email is not in use. Starting OTA");
StartCoroutine(sendOTA()); // Start the OTP process
OTARead.SetActive(true);
}
else
{
Debug.Log("The account exists");
accountExists.SetActive(true);
}
rdr.Close();
conn.Close();
}
您的主要问题是结果集没有行,因此 rdr.Read()
returns 第一个 运行 为 false 并且从不执行循环。
如果 email
列是唯一的,那么您只需要一个结果。为此,您可以避免 reader 并仅使用 cmd.ExecuteScalar
- 注意正确使用参数化,防止危险的SQL注入
- 注意
using
块,这将保证在发生异常时关闭连接
bool doesEmailExist = false;
const string seecQuery = @"
SELECT 1
FROM accounts
WHERE email = @email;
";
Debug.Log(seecQuery);
using (var conn = new MySqlConnection(yourConnString))
using (var cmd = new MySqlCommand(seecQuery, conn))
{
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
Debug.Log("SUCCESSFULL CONNECTION!");
Debug.Log("Checking to see if the account exists");
if (cmd.ExecuteScalar() == (object)1)
{
Debug.Log("The account exists");
accountExists.SetActive(true);
//doesEmailExist = true;
}
else
{
Debug.Log("Email is not in use. Starting OTA");
StartCoroutine(sendOTA()); // Start the OTP process
OTARead.SetActive(true);
}
}
顾名思义,这是 Reader 的 while 循环仅在 运行 返回值时出现的问题。在这种情况下见下文,在 if (email == rdr[0].ToString())
returns true 的情况下,while 循环本身会执行。但是,如果它不是 true,则 else 不会执行。同样重要的是要注意, Debug.Log("Checking to see if the account exists");
只有在帐户存在的情况下才会实际执行,正如支票所希望的那样。
我的假设是,如果连接器找不到一行,它不会 运行 循环。我怎样才能实现这个 运行ning?
谢谢。
conn.Open();
Debug.Log("SUCCESSFULL CONNECTION!");
if (!isLogin)
{
Debug.Log("Is not login");
bool doesEmailExist = false;
string seecQuery = "SELECT * FROM accounts WHERE email = '"+ email +"'";
Debug.Log(seecQuery);
MySqlCommand cmd = new MySqlCommand(seecQuery, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Debug.Log("Checking to see if the account exists");
if (email == rdr[0].ToString())
{
Debug.Log("The account exists");
accountExists.SetActive(true);
//doesEmailExist = true;
}
else //if (!doesEmailExist)
{
Debug.Log("Email is not in use. Starting OTA");
StartCoroutine(sendOTA()); // Start the OTP process
OTARead.SetActive(true);
}
这只是一个更新,我设法通过解决方法解决了这个问题。 这里是。我没有执行 while 循环,而是检查 Reader 是否返回了任何行。工作正常。
conn.Open();
Debug.Log("SUCCESSFULL CONNECTION!");
if (!isLogin)
{
Debug.Log("Is not login");
bool doesEmailExist = false;
string seecQuery = "SELECT * FROM accounts WHERE email = '"+ email +"'";
Debug.Log(seecQuery);
MySqlCommand cmd = new MySqlCommand(seecQuery, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
if (!rdr.HasRows)
{
Debug.Log("Email is not in use. Starting OTA");
StartCoroutine(sendOTA()); // Start the OTP process
OTARead.SetActive(true);
}
else
{
Debug.Log("The account exists");
accountExists.SetActive(true);
}
rdr.Close();
conn.Close();
}
您的主要问题是结果集没有行,因此 rdr.Read()
returns 第一个 运行 为 false 并且从不执行循环。
如果 email
列是唯一的,那么您只需要一个结果。为此,您可以避免 reader 并仅使用 cmd.ExecuteScalar
- 注意正确使用参数化,防止危险的SQL注入
- 注意
using
块,这将保证在发生异常时关闭连接
bool doesEmailExist = false;
const string seecQuery = @"
SELECT 1
FROM accounts
WHERE email = @email;
";
Debug.Log(seecQuery);
using (var conn = new MySqlConnection(yourConnString))
using (var cmd = new MySqlCommand(seecQuery, conn))
{
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
Debug.Log("SUCCESSFULL CONNECTION!");
Debug.Log("Checking to see if the account exists");
if (cmd.ExecuteScalar() == (object)1)
{
Debug.Log("The account exists");
accountExists.SetActive(true);
//doesEmailExist = true;
}
else
{
Debug.Log("Email is not in use. Starting OTA");
StartCoroutine(sendOTA()); // Start the OTP process
OTARead.SetActive(true);
}
}