设计的本意是:点击btnRegister按钮 就查找数据库是否有跟tbUserName相同若有提示 “该用户已存在,请重新输入用户名” 如没有相同则 可以注册。 高手帮忙看看哪里错了 我数据库是 id(自增长)UserName Password。
private void btnRegister_Click(object sender, EventArgs e)
{ bool isEmpty = Check();
if (isEmpty == true)
{
int count;
string same = string.Format("select count(*) from Users where UserName='{0}'", tbUserName.Text.Trim());
string sql = string.Format("insert into Users values('{0}','{1}')", tbUserName.Text.Trim(), tbPassword.Text.Trim());
SqlCommand samename = new SqlCommand(same, DBhelper.connection);
DBhelper.connection.Open();
count = samename.ExecuteNonQuery();
DBhelper.connection.Close();
if (count < 1)
{
SqlCommand cmd = new SqlCommand(sql, DBhelper.connection);
DBhelper.connection.Open();
count = cmd.ExecuteNonQuery();
DBhelper.connection.Close();
if (count == 1)
{ MessageBox.Show("注册成功!");
this.Hide();
LForm.GetUserName(tbUserName.Text.Trim());
LForm.Show();
}
}
else
{
MessageBox.Show("该用户已存在,请重新输入用户名");
}
}
}
private void btnRegister_Click(object sender, EventArgs e)
{ bool isEmpty = Check();
if (isEmpty == true)
{
int count;
string same = string.Format("select count(*) from Users where UserName='{0}'", tbUserName.Text.Trim());
string sql = string.Format("insert into Users values('{0}','{1}')", tbUserName.Text.Trim(), tbPassword.Text.Trim());
SqlCommand samename = new SqlCommand(same, DBhelper.connection);
DBhelper.connection.Open();
count = samename.ExecuteNonQuery();
DBhelper.connection.Close();
if (count < 1)
{
SqlCommand cmd = new SqlCommand(sql, DBhelper.connection);
DBhelper.connection.Open();
count = cmd.ExecuteNonQuery();
DBhelper.connection.Close();
if (count == 1)
{ MessageBox.Show("注册成功!");
this.Hide();
LForm.GetUserName(tbUserName.Text.Trim());
LForm.Show();
}
}
else
{
MessageBox.Show("该用户已存在,请重新输入用户名");
}
}
}
使用 int Count=(int)cmd.ExecuteScalar();
{
if (TesterIdTextBox.Text == "")
{
MessageBox.Show("账号不能为空");
return;
}
else if (TesterNameTextBox.Text == "")
{
MessageBox.Show("实验员不能为空");
return;
}
else if (PwdTextBox.Text == "")
{
MessageBox.Show("密码不能为空");
return;
}
Maticsoft.Model.Tester a_tester = new Maticsoft.Model.Tester(); if (check.GetModel(TesterIdTextBox.Text) == null)
{
a_tester.TesterId = TesterIdTextBox.Text;
a_tester.TesterName = TesterNameTextBox.Text;
a_tester.TesterPwd = PwdTextBox.Text;
check.Add(a_tester);
UpdataGrid(); }
else
{
MessageBox.Show("账号已被占用", "信息提示", MessageBoxButtons.OK);
}
}
这是我最近在搞的一些代码,你用三层架构,会简单很多的