110,536
社区成员
发帖
与我相关
我的任务
分享
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空!");
return;
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空!");
return;
}
try
{
string connString = "Data Source=(local);Initial Catalog=text;Integrated Security=True";
string sql = string.Format("select count(*) from users where name='{0}'",textBox1.Text);
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
int a = (int)cmd.ExecuteScalar();
StringBuilder strsql = new StringBuilder();
if (a == 0)
{
strsql.Append("insert into users(name,pwd)");
strsql.Append("values(");
strsql.Append("'" + textBox1.Text.Trim().ToString() + "',");
strsql.Append("'" + textBox2.Text.Trim().ToString() + "'");
strsql.Append(")");
using (SqlCommand cmd2 = new SqlCommand(strsql.ToString(), conn))
{
cmd2.ExecuteNonQuery();
}
MessageBox.Show("注册成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Form1 f1 = new Form1();
f1.Show();
this.Hide();
this.Close();
}
else
{
MessageBox.Show("用户已存在!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Application.Exit();
}
简单粗暴,改一下代码的位置