/// <summary> /// 第一步:登陆验证方法;7章178页,参考;过滤不正确的数据. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public bool ValidateInput() { if (txtNickName.Text.Trim().Equals("")) //去除两端空格. { MessageBox.Show("昵称不能为空","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information); txtNickName.Focus(); return false; } if (txtLoginPwd.Text.Trim().Equals("")) { MessageBox.Show("密码不能为空", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); txtLoginPwd.Focus(); return false; } if (!txtLoginPwd.Text.Trim().Equals(txtLoginPwdAgain.Text.Trim())) { MessageBox.Show("两次密码不一致", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); txtLoginPwd.Focus(); return false; }
<span style="white-space:pre"> </span>//可以加入不允许输入'和--等SQL 字符,此处省略了 return true; } /// <summary> /// 第三部,插入数据代码;下面三项是可选项! /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRegist_Click(object sender, EventArgs e) { string star, bloodtype; int qqNum; //申请的QQ号; string message; //消息变量 if (ValidateInput()) //第3步:Ok,数据都符合 {//执行插入语句 if (cboStar.Text != "" && cboBloodType.Text != "") //全插入命令 { star = (cboStar.SelectedIndex + 1).ToString(); bloodtype = (cboBloodType.SelectedIndex + 1).ToString(); } else if (cboStar.Text != "" && cboBloodType.Text != "") { star = (cboStar.SelectedIndex + 1).ToString(); bloodtype = "null"; } else if (cboStar.Text == "" && cboBloodType.Text != "") { star = "null"; bloodtype = (cboBloodType.SelectedIndex + 1).ToString(); } else { star = "null"; bloodtype = "null"; } string sql = string.Format("insert users(LoginPwd, NickName, Sex, Age, Name, StarId, BloodTypeId) values('{0}','{1}','{2}',{3},'{4}',{5},{6})", txtLoginPwd.Text.Trim(), txtNickName.Text.Trim(), rdoMale.Checked ? "男" : "女", nudAge.Value,txtName.Text.Trim(),star, bloodtype); SqlCommand comm = new SqlCommand(sql, DBHelper.conn); //将sql语句调出,看下出了问题没有 DBHelper.conn.Open(); int count = comm.ExecuteNonQuery(); //执行增 删 改 if (count > 0) { sql = "select @@Identity from users"; //执行insert之后,会在内存当中出来一个变量 comm.CommandText = sql; qqNum = Convert.ToInt32(comm.ExecuteScalar()); message = string.Format("您申请的QQ号码是:{0}",qqNum); } else { message = "注册失败"; } MessageBox.Show(message,"提示信息"); } } /// <summary> /// 第2步:窗体加载代码,写一段代码,从数据库里面,把数据读取到两个下拉框; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RegisterForm_Load(object sender, EventArgs e) {//考察知识点:SqlDataReader string sql = string.Format("select * from star"); DBHelper.conn.Open(); SqlCommand comm = new SqlCommand(sql,DBHelper.conn); SqlDataReader sdr = comm.ExecuteReader(); //sdr:临时表 while(sdr.Read()){ //cboStar.Items.Add(sdr[1].ToString()); cboStar.Items.Add(sdr.GetString(1)); //两种方式都可以 }//end。结束while sdr.Close(); DBHelper.conn.Close(); //*********************************添加血型表数据
//可以使用断开式连接,在此没有使用! sql = "select * from BloodType"; comm.CommandText = sql; DBHelper.conn.Open(); sdr = comm.ExecuteReader(); while (sdr.Read()) { //cboStar.Items.Add(sdr[1].ToString()); cboBloodType.Items.Add(sdr.GetString(1)); //两种方式都可以 }//end。结束while sdr.Close(); DBHelper.conn.Close(); //************************************** }
跳转到选择头像窗体
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MYQQ { /// <summary> /// 头像选择窗体 /// </summary> public partial class FacesForm : Form { //1个人信息窗体,作为要回传的对象; public PersonalInfoForm personalInfoForm; // 个人信息窗体,成员变量,将来回传数据 pif //*****************************别动 public FacesForm() { InitializeComponent(); } //*****************************星号内别动 //2窗体加载时显示头像图片,窗体加载事件。 private void FacesForm_Load(object sender, EventArgs e) { for (int i = 0; i < ilFaces.Images.Count; i++) { lvFaces.Items.Add(i.ToString()); //0 1 2 3 4 5 lvFaces.Items[i].ImageIndex = i; //每一项的图片索引是i //ListViewItem lvi = new ListViewItem(); // listView的每一项,是一个ListViewItem //lvi.Tag = i; //隐藏标志 //lvi.ImageIndex = i; //lvFaces.Items.Add(lvi); } } //3确定选择头像,单击Ok按钮代码; private void btnOK_Click(object sender, EventArgs e) { if (lvFaces.SelectedItems.Count == 0) { MessageBox.Show("请选择一个头像!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { int faceId = lvFaces.SelectedItems[0].ImageIndex; // 获得当前选中的头像的索引 personalInfoForm.ShowFace(faceId); // 设置 个人信息窗体 中显示的头像 this.Close(); } } //5关闭窗体 private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } //4双击时选择头像 private void lvFaces_MouseDoubleClick(object sender, MouseEventArgs e) { int faceId = lvFaces.SelectedItems[0].ImageIndex; // 获得当前选中的头像的索引 personalInfoForm.ShowFace(faceId); // 设置个人信息窗体中显示的头像 this.Close(); } } }