注册界面,发现不用输入密码就能直接注册,想问一下注册的时候必须要输入用户名密码要改哪里?

weixin_41610471 2019-07-25 10:24:52
private void button1_Click(object sender, EventArgs e)
{

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
{
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空!");
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空!");
}
else
{
MessageBox.Show("用户已存在!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Form2 f2 = new Form2();
f2.Show();
this.Hide(); }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Application.Exit();
}
}
}
...全文
159 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LvBao_117 2019-07-25
  • 打赏
  • 举报
回复
在注册页面添加这两项
听雨停了 2019-07-25
  • 打赏
  • 举报
回复

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();
            }
简单粗暴,改一下代码的位置
java__net 2019-07-25
  • 打赏
  • 举报
回复
如果有了用户名和密码,为什么还要注册?
楠小南 2019-07-25
  • 打赏
  • 举报
回复
哇塞,短短一段代码就那么多问题,你这个好歹一开始判断非空吧,难道你去买菜的时候不用看看带钱了没有吗

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧