public string connstr = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
private void btnIn_Click(object sender, EventArgs e)
{
strError = "";
if (this.txtName.Text == "" || this.txtPwd.Text == "")
{
MessageBox.Show("输入不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (this.txtName.Text != "" && this.txtPwd.Text != "")
{
if (this.txtName.Text.Length > 10)
{
strError += "用户名输入字符数应小于10位!" + "\r";
}
if (this.txtPwd.Text.Length > 10)
{
strError += "密码输入字符数应小于10位!" + "\r";
}
if (strError != "")
{
MessageBox.Show(strError.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
try
{
SqlConnection conn = new SqlConnection(connstr);
if (conn.State == ConnectionState.Closed)
{
try
{
conn.Open();
}
catch (Exception)
{
MessageBox.Show("请检查数据库数否连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
string sql = "select * from UserIn where UserName = '" + this.txtName.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("已存在此用户!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
}
else
{
string addstr = "insert into UserIn(UserName,PassWord) values('" +this.txtName.Text.Trim() + "','" + this.txtPassWord.Text.Trim() + "')";
SqlCommand sqlcom = new SqlCommand(addstr, conn);
try
{
sqlcom.ExecuteNonQuery();
MessageBox.Show("注册成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("注册失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
}
}
conn.Close();
}
catch
{
}
}
}
}
[[it] 本帖最后由 谁与争瘋 于 2008-11-18 16:34 编辑 [/it]]