简体   繁体   English

数据库更新不起作用

[英]Database Update doesn't work

As in title. 如标题。 I tried to do everything, I searched on internet everywhere but it doesn't work. 我试图做所有事情,我在互联网上搜索,但它不起作用。 Here's the code: 这是代码:

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);
        }

    }

I Correctly put both of values into Strings IP and Username. 我正确地将两个值放入字符串IP和用户名。 I get Username from TextBox and IP adress by doing this code: 我通过执行以下代码从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;
    }

And I just call method SetIP like this: SetIP(GetIP(), UsernameBox.Text); 我只是像这样调用方法SetIP:SetIP(GetIP(),UsernameBox.Text); But when I come to the database to check if it changed it's still the same. 但是,当我来到数据库检查它是否改变它仍然是相同的。 All the time. 每时每刻。

//Edit: Got this error command: "There is already an open DataReader associated with this Connection which must be closed first." //编辑:得到此错误命令:“已经有一个与此Connection关联的打开DataReader,必须先关闭它。”

I use this DataReaders: 我使用这个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;
        } 
    }

I'm using only "LoginSystem_FindUser" and after that SetIP, FindUser and FindEmail I use only for registration. 我只使用“LoginSystem_FindUser”,之后我只使用SetIP,FindUser和FindEmail进行注册。

'Username' = '@user'

will always return to false because it compares literally. 将永远返回false,因为它按字面比较。

It's because you parameters were wrapped with single quotes. 这是因为你的参数是用单引号包装的。 Remove the single quotes and it will work. 删除单引号,它将起作用。

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

One more thing, column names are identifiers so they should also not be surrounded with single quotes. 还有一件事,列名是标识符,所以它们也不应该用单引号括起来。

I got it. 我知道了。 I forgot 我忘了

connectionReader.Close(); connectionReader.Close();

after if statement. 在if声明之后。 Thanks BTW. 谢谢BTW。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM