0

我正在开发一个有 1 个客户端和 1 个服务器的项目。我还创建了一个类库,我将代码放入其中,然后将该类库作为服务器和客户端的参考。所以我通过创建类的对象来调用客户端的方法。它工作得非常好......直到现在!

在类库中,我有一个名为 Check() 的方法,它检查用户名是否已经存在(在尝试注册时,以及其他一些东西。但问题是它从数据库中检查用户名的步骤被跳过了!!我不知道为什么,它没有显示错误原因。出现的唯一错误是由于跳过的步骤而尝试将相同的用户名插入数据库时​​出现的错误。

这是类库代码:

    bool busy = false;
    bool empty = false;
    bool notSame = false;
    bool completed = false;

public void Check(string a1, string b2, string c3, string d4, string e5, string f6)
    {
        SqlConnection con = new SqlConnection(@"Data Source=TALY-PC;Initial Catalog=TicTacToe;Integrated Security=True");

        SqlCommand cmd = new SqlCommand("SELECT * FROM tblUsers", con);

        if (con.State.Equals(ConnectionState.Open))
        {
            return;
        }
        else
        {
            con.Open();
        }

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {

            if (dr["Username"].ToString() == d4)
            {
                busy = true;
            }
            else if (a1 == "" || b2 == "" || c3 == "" || d4 == "" || e5 == "" || f6 == "")
            {

                empty = true;

            }
            else if (e5 != f6)
            {
                notSame = true;
                return;
            }
            else
            {
                dr.Close();
                SqlCommand cmd1 = new SqlCommand("INSERT INTO tblUsers (Name, LastName, email, Username, Password) VALUES ('" + a1 + "', '" + b2 + "', '" + c3 + "', '" + d4 + "', '" + e5 + "')", con);

                cmd1.ExecuteNonQuery();

                completed = true;
            }
        }

跳过的部分是这部分:

if (dr["Username"].ToString() == d4)
            {
                busy = true;
            }

我不知道为什么它不检查这部分?

这是来自客户端的代码:

HttpChannel chan = new HttpChannel();

    Tic obj = (Tic)Activator.GetObject(typeof(Tic), "http://127.0.0.1:9050/MyMathServer");

private void RegisterForm_Load(object sender, EventArgs e)
    {
        ChannelServices.RegisterChannel(chan);
    }

 private void Button1_Click(object sender, EventArgs e)
   {
 obj.Check(textBoxX1.Text, textBoxX2.Text, textBoxX3.Text, textBoxX4.Text, textBoxX5.Text, textBoxX6.Text);
        label8.Text = obj.getUseri();
        if (obj.getBusy() == true)
        {
            MessageBox.Show("This username is already Taken, please choose another username");

            obj.setBusy();

        }
        else if (obj.getEmpty() == true)
        {
            MessageBox.Show("Please fill all the fields!");
            obj.setEmpty();
        }
        else if (obj.getNotSame() == true)
        {
            MessageBox.Show("Passwords don't match!!");
            textBoxX5.Text = "";
            textBoxX6.Text = "";
            obj.setNotSame();
        }
        else if (obj.getCompleted() == true)
        {
            MessageBox.Show("Registration was completed successfully. Please close the window and Log Int ");
            textBoxX1.Text = "";
            textBoxX2.Text = "";
            textBoxX3.Text = "";
            textBoxX4.Text = "";
            textBoxX5.Text = "";
            textBoxX6.Text = "";
            obj.setCompleted();

        }
  }

有人可以告诉我为什么不检查用户名吗?

4

1 回答 1

1

我不完全理解你的代码。我可以做一些简单的观察:

  • 首先:使用using您的连接和数据阅读器。无论发生什么(异常等),using都会关闭它们。
  • 为变量使用更好的名称。a1直到f6毫无意义。给它们起有意义的名字使我们更容易阅读......所以......如果真的不知道为什么e5必须等于f6。
  • 你的文本框也是如此。
  • 该语句if (a1 == "" || b2 == "" || c3 == "" || d4 == "" || e5 == "" || f6 == """)在 while 循环内,但由于这些值永远不会改变,您可能会考虑将它们移出循环。
  • 线路也是如此if (e5 != f6)
  • 您检查连接是否打开。但这种联系刚刚建立。怎么可能打开?如果它会打开......它会简单地返回而不做任何事情?
  • 如果您达到条件(e5 != f6)并且条件为真,则返回。但是你这样做没有关闭连接。
  • 如果所有参数都是"",但数据库中没有记录,empty则不会设置。这是设计使然吗?

试试这个代码:

public enum Result
{
    Busy,
    Empty,
    NotSame,
    Completed
}

public Result Check(string name, string lastName, string eMail, string userName, string password1, string password2)
{
    // You should check with String.IsNullOrEmpty(...), not just an empty string.
    if (name == "" || lastName == "" || eMail == "" || userName == "" || password1 == "" || password2 == "")
        return Result.Empty;

    if (password1 != password2)
        return Result.NotSame;

    using(SqlConnection con = new SqlConnection(@"Data Source=TALY-PC;Initial Catalog=TicTacToe;Integrated Security=True"))
    {
        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM tblUsers WHERE UserName=@0", con);
        cmd.Parameters.AddWithValue("@0", userName);
        int count = (int)cmd.ExecuteScalar();
        if (count > 0)
            return Result.Busy;

        // I must admit, also the insert values should be done with SqlParameters.
        cmd = new SqlCommand("INSERT INTO tblUsers (Name, LastName, email, Username, Password) VALUES ('" + name + "', '" + lastName + "', '" + eMail + "', '" + userName + "', '" + password1 + "')", con);
        cmd.ExecuteNonQuery();
        return Result.Completed;
    }
}
于 2013-04-28T00:16:44.417 回答