public const string sqlconn = "Data Source=.;Initial Catalog=hh;Integrated Security=True";
private void butenroa_Click(object sender, EventArgs e)
{
//接受输入
string name = this.texenroa.Text;
string NAME = this.texenrob.Text;
string code = this.texenroc.Text;
string code1 = this.texenrod.Text;
string wenti = this.comboBoxenro1.Text;
string mibao = this.texenroe.Text;
if (name == "" | code == "" | code1 == "" | mibao == ""|wenti==""|NAME=="")
{
MessageBox.Show("注册失败,任何一项不能为空");
return;
}
else if (wenti == "请选择密保问题?")
{
MessageBox.Show("请选择密保问题!");
return;
}
else if (code != code1)
{
MessageBox.Show("密码前后不一致");
return;
}
else if (name.Length > 16 | name.Length < 5)
{
MessageBox.Show("用户名必须是 5-16位");
return;
}
if (!Regex.IsMatch(code, @"^[a-zA-Z0-9]{6,16}$"))
{
MessageBox.Show("密码格式错误,只能是6-16位数字字母的组合!");
return;
}
//打开数据库
SqlConnection conn = new SqlConnection(sqlconn);
conn.Open();
//用户名是主键,检索y用户名是否存在
string sql = "select count(*) from hh where y_name='" + name + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
int i = (int)cmd.ExecuteScalar();
if (i > 0)
{
MessageBox.Show("此用户已存在,请重新注册");
return;
}
//满足条件,允许注册
sql = "insert into hh values('" + name + "','" + NAME + "','" + code + "','" + wenti + "','" + mibao + "','"+""+"')";
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("请记住您的密码问题:" + wenti + "【" + mibao + "】" + ",这是您找回密码的唯一途径");
DialogResult dr = MessageBox.Show("注册成功,是否登录?\n【是】转到登录\n【否】继续注册","选择",MessageBoxButtons.YesNo,MessageBoxIcon.None);
if (dr == DialogResult.Yes) //返回登录
{
Close();
}
else //继续注册
{
this.texenroa.Text = "";
this.texenrob.Text = "";
this.texenroc.Text = "";
this.texenrod.Text = "";
this.texenroe.Text = "";
this.comboBoxenro1.Text = "请选择密保问题?";
}
追问你这从哪儿弄过来的啊,跟我问的不搭界啊
追答呵呵 ,你想要我把你的那写代码写出来啊?这是个连接的很简单的例子,我中午刚写的,和你的这个事例是一样的!好好看看就懂啦!