如标题中所示。我尝试了一切,在互联网上到处搜索,但是没有用。这是代码:

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。

最佳答案

'Username' = '@user'


将始终返回false,因为它会进行字面比较。

这是因为参数用单引号引起来。删除单引号,它将起作用。

String commandString = "UPDATE `Users` SET `IP` = @ip WHERE Username = @user;";


还有一件事,列名是标识符,因此它们也不应用单引号引起来。

关于c# - 数据库更新不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18992048/

10-11 03:38