该功能用于在用户注册过程中检查用户名是否已被他人使用,确保每个用户的用户名都是独一无二的。
```csharp
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox1.Text) || this.textBox1.Text == 请输入用户名:)
{
MessageBox.Show(用户名不能为空!);
}
string uname = this.textBox1.Text;
string pwd = this.textBox3.Text;
hyqpm.con.Close();
hyqpm.con.Open();
SqlCommand com = new SqlCommand($insert into admin values({uname}, {pwd}), hyqpm.con);
if (checkuName(uname)) // 判断用户名是否已存在
{
MessageBox.Show(用户名已存在!请重新输入:);
return;
}
if (string.IsNullOrEmpty(this.textBox1.Text) || string.IsNullOrEmpty(this.textBox2.Text))
{
MessageBox.Show(密码不能为空!);
return;
}
if (com.ExecuteNonQuery() > 0)
{
MessageBox.Show(提交成功!);
}
hyqpm.con.Close();
}
// 判断用户名是否存在
private bool checkuName(string name)
{
bool flag = false;
SqlCommand com1 = new SqlCommand($select * from admin where loginid={this.textBox1.Text}, hyqpm.con);
SqlDataReader dr1 = com1.ExecuteReader();
if (dr1.Read())
flag = true;
dr1.Close();
return flag;
}
```