如标题所示。我试图做所有事情,我在互联网上搜索,但它不起作用。这是代码:
public void SetIP(String IP, String Username)
{
try
{
String commandString = "UPDATE `Users` SET `IP` = '@ip' WHERE 'Username' = '@user';";
command = new MySqlCommand(commandString, connection);
command.Parameters.AddWithValue("@ip", IP);
command.Parameters.AddWithValue("@user", Username);
command.BeginExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
我正确地将两个值都放入字符串IP和用户名中。我通过执行以下代码从TextBox和IP地址获取用户名:
public String GetIP()
{
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);
return direction;
}
我只是像这样调用方法SetIP:SetIP(GetIP(),UsernameBox.Text); 但是,当我来到数据库检查它是否改变它仍然是相同的。一直以来。
//编辑: 得到此错误命令:“已经有一个与此Connection关联的打开DataReader,必须先关闭它。”
我使用这个DataReaders:
public bool FindUsername(String Username)
{
String commandString = "select * from Users where Username = '" + Username + "';";
command = new MySqlCommand(commandString, connection);
MySqlDataReader connectionReader = command.ExecuteReader();
if (connectionReader.Read())
{
connectionReader.Close();
return true;
}
else
{
connectionReader.Close();
return false;
}
}
public bool FindEmail(String Email)
{
String commandString = "select * from Users where Email = '" + Email + "';";
command = new MySqlCommand(commandString, connection);
MySqlDataReader connectionReader = command.ExecuteReader();
if (connectionReader.Read())
{
connectionReader.Close();
return true;
}
else
{
connectionReader.Close();
return false;
}
}
public bool LoginSystem_FindUser(String Username, String Password)
{
String commandString = "select * from Users where Username = '"+Username+"' and Password = '"+Password+"' ;";
command = new MySqlCommand(commandString, connection);
MySqlDataReader connectionReader = command.ExecuteReader();
if (connectionReader.Read())
{
return true;
}
else
{
connectionReader.Close();
return false;
}
}
我只使用“LoginSystem_FindUser”,之后只使用SetIP,FindUser和FindEmail进行注册。
答案 0 :(得分:2)
'Username' = '@user'
将始终返回false,因为它按字面比较。
这是因为你的参数是用单引号包装的。删除单引号,它将起作用。
String commandString = "UPDATE `Users` SET `IP` = @ip WHERE Username = @user;";
还有一件事,列名是标识符,所以它们也不应该用单引号括起来。
答案 1 :(得分:0)
我明白了。我忘了
connectionReader.Close();
在if语句之后。谢谢BTW。