我正在开发一个有 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();
}
}
有人可以告诉我为什么不检查用户名吗?