C# SqlDataReader不是';如果不工作,它会跳过检查用户名的部分
我在一个有一个客户端和一个服务器的项目上工作。我还创建了一个类库,我将代码放在其中,然后将这个类库作为服务器和客户端的引用。因此,我通过创建类的对象从客户机调用方法。 而且效果非常好。。。直到现在 在类库中,我有一个名为Check()的方法,它检查用户名是否已经存在(当尝试注册时,以及其他一些东西。但是问题是它从数据库检查用户名的步骤被跳过了!!我不知道为什么,它没有向我显示错误原因。出现的唯一错误是由于跳过了该步骤而在尝试将同一用户名插入数据库时出现的错误 以下是类库代码:C# SqlDataReader不是';如果不工作,它会跳过检查用户名的部分,c#,visual-studio-2010,sqldatareader,C#,Visual Studio 2010,Sqldatareader,我在一个有一个客户端和一个服务器的项目上工作。我还创建了一个类库,我将代码放在其中,然后将这个类库作为服务器和客户端的引用。因此,我通过创建类的对象从客户机调用方法。 而且效果非常好。。。直到现在 在类库中,我有一个名为Check()的方法,它检查用户名是否已经存在(当尝试注册时,以及其他一些东西。但是问题是它从数据库检查用户名的步骤被跳过了!!我不知道为什么,它没有向我显示错误原因。出现的唯一错误是由于跳过了该步骤而在尝试将同一用户名插入数据库时出现的错误 以下是类库代码: bool
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();
}
}
有人能告诉我为什么不检查用户名吗?我不完全理解你的代码。我可以做一些简单的观察:
- 首先:使用连接和数据阅读器的
。无论发生什么情况(异常等),using
都会关闭它们using
- 为变量使用更好的名称。
tilla1
没有任何意义。给它们取有意义的全名让我们更容易阅读…所以…如果真的不知道为什么e5必须等于f6f6
- 同样的事情也适用于你的文本框
- 语句(A1==“”、“B2==”、“ωc3==”、“ωd4==”、“{ E5==”、“ωf6==“”、<代码>在while循环内,但由于这些值从未改变,所以您可以考虑将它们移出循环。
- 如果(e5!=f6),则行
也是如此
- 您检查连接是否打开。但此连接刚刚创建。它如何打开?如果它将打开…它将直接返回而不执行任何操作
- 如果您达到条件
,并且该条件为真,则返回。但您这样做时不会关闭连接(e5!=f6)
- 如果所有参数都是
,但数据库中没有记录,”
将不会设置为空。这是设计的吗
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;
}
}
我建议为方法参数和文本框指定描述性名称,以便更容易理解逻辑。可能您混淆了参数的顺序。在debugger中逐步执行…@MitchWheat,我使用了一个变量并执行了此操作:
string test=busy.ToString()
('busy'是bool变量)我使用“busy”来查看它是否忙,而不是将其设置为true
,如果不将其设置为false
。当我通过getMethod()调用该变量时,即使我使用了数据库中存在的用户名,也显示为false!不过,似乎您正在请求某种类型的web服务来检查用户名数据(!),您没有返回来自同一调用的结果;相反,您正在将结果缓存在此web服务中,并在稍后轮询。在此期间,web服务可能已被回收(无论两次调用之间的时间有多短)。您的数据库中的“Username”正确吗?还是“Username”或“Username”“?可能吗?我会添加use string.IsNullOrEmpty(名称)来代替name==”“对于每个变量。如果用户在字段中键入一个空格,它将通过。完全正确…但由于我已经注释了那么多,我不确定是否还要注释更多。因为他使用文本框,参数将永远不会为nul…在这种情况下。但我会调整答案。Thanx!Ey@MartinMulder,thnx alot bro…你帮了我很多忙!!我不得不这么做在你的代码中做一些更改,它就工作了…thnx man:D