如标题。我试图做所有事情,我在互联网上到处搜索,但它不起作用。这是代码:
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); 但是当我来到数据库检查它是否改变时,它仍然是一样的。每时每刻。
//编辑:得到这个错误命令:“已经有一个打开的 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 进行注册。