我正在为论文制作一个简单的登录页面,并且我已经完成了字符长度和密码不匹配验证..我的问题是如何测试给定的用户名是否已经存在于我的数据库中......我我用 C# 编码并为我的数据库使用 SQL 管理工作室 R2....
private void add_Click(object sender, EventArgs e)
{
string UserName = textBox1.Text;
string Password = maskedTextBox1.Text;
if (Password.Length <= MiN_LENGHT && UserName.Length <= MiN_LENGHT)
{
errorProvider1.SetError(textBox1, "User name must be at least 8 character");
errorProvider2.SetError(maskedTextBox1, @"Password must be at least 8 character");
maskedTextBox1.Clear();
maskedTextBox2.Clear();
}
else if (maskedTextBox1.Text != maskedTextBox2.Text)
{
errorProvider1.SetError(maskedTextBox2, "Passwords don't match");
maskedTextBox1.Clear();
maskedTextBox2.Clear();
}
else if (textBox1.Text == "" || maskedTextBox1.Text == "" ||
maskedTextBox2.Text == "")
{
MessageBox.Show("Please fill up the required records", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
x.da.InsertCommand = new SqlCommand(@"Insert into PlayerList
VALUES(@uname,@pw,@repw)", x.cs);
x.da.InsertCommand.Parameters.Add("@uname", SqlDbType.NVarChar).Value =
textBox1.Text;
x.da.InsertCommand.Parameters.Add("@pw", SqlDbType.NVarChar).Value =
maskedTextBox1.Text;
x.da.InsertCommand.Parameters.Add("@repw", SqlDbType.NVarChar).Value =
maskedTextBox2.Text;
x.cs.Open();
x.da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Record Added", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
button3.Enabled = true;
x.da.SelectCommand = new SqlCommand( @"Select PlayerCode, uname from
PlayerList", x.cs);
x.ds.Clear();
x.da.Fill(x.ds);
dataGridView1.DataSource = x.ds.Tables[0];
x.cs.Close();
}
}
希望你能帮助....