using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyQQ
{
public partial class PersonalInfoForm : Form
{
public PictureBox picture = null;
public PersonalInfoForm()
{
InitializeComponent();
}
//个人资料
private void a_Click(object sender, EventArgs e)
{
pnlBaseInfo.Visible = true;
pnlSecurity.Visible = false;
btnBaseInfo.Visible = false;
btnSecurity.Visible = true;
}
//安全设置
private void b_Click(object sender, EventArgs e)
{
pnlBaseInfo.Visible = false;
pnlSecurity.Visible = true;
btnBaseInfo.Visible = true;
btnSecurity.Visible = false;
}
//退出
private void TsmiExit_Click(object sender, EventArgs e)
{
this.Close();
}
//跳转头像
private void btnNext_Click(object sender, EventArgs e)
{
FacesForm fom = new FacesForm();
fom.pic = this.picHead;
fom.Show();
}
//窗体加载
private void PersonalInfoForm_Load(object sender, EventArgs e)
{
pnlBaseInfo.Location = pnlSecurity.Location;
btnBaseInfo.Visible = false;
//使显示“个人资料”的Panel可见
pnlBaseInfo.Visible = true;
//使显示“安全设置”的按钮可见
btnSecurity.Visible = true;
//使显示“安全设置”的Panel不可见
pnlSecurity.Visible = false;
try
{
//加载星座
string sql = "select star from star";
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
cboStar.Items.Add(reader["star"].ToString());
}
reader.Close();
//加载血型
command.CommandText = "select BloodType from BloodType";
reader = command.ExecuteReader();
while (reader.Read())
{
cboBloodType.Items.Add(reader["BloodType"].ToString());
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
try
{
string sqll = string.Format("select * from users where id = {0} and loginpwd='{1}'", UserHeper.loginId, UserHeper.loginPwd);
SqlCommand com = new SqlCommand(sqll, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader datar = com.ExecuteReader();
if (datar.Read())
{
txtOldPwd.Text = datar["loginpwd"].ToString();
txtUserId.Text = datar["id"].ToString();
txtUserNickName.Text = datar["nickname"].ToString();
cboSex.Text = datar["sex"].ToString();
txtAge.Text = datar["age"].ToString();
int i = Convert.ToInt32(datar["faceid"]);
picHead.Image = Ivface.Images[i];
if (!(datar["name"] is DBNull))
{
txtName.Text = datar["name"].ToString();
}
if (!(datar["starid"] is DBNull))
{
UserHeper.starid = Convert.ToInt32(datar["starid"]);
}
if (!(datar["bloodtypeid"] is DBNull))
{
UserHeper.bloodtypeid = Convert.ToInt32(datar["bloodtypeid"]);
}
}
datar.Close();
//星座id
sqll = string.Format("select star from star where id={0}", UserHeper.starid);
com.CommandText = sqll;
datar = com.ExecuteReader();
if (datar.Read())
{
cboStar.Text = datar["star"].ToString();
}
datar.Close();
//血型id
sqll = string.Format("select bloodtype from bloodtype where id={0}", UserHeper.bloodtypeid);
com.CommandText = sqll;
datar = com.ExecuteReader();
if (datar.Read())
{
cboBloodType.Text = datar["bloodtype"].ToString();
}
datar.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
//获取 星座Id
private int StarId()
{
int starId = 0; //星座Id值
string sql = string.Format("select Id from Star where Star='{0}'", cboStar.Text);
try
{
//定义 Command 对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open(); //打开数据库连接
SqlDataReader dataReader = command.ExecuteReader(); //执行查询
//读取班级 Id
if (dataReader.Read())
{
starId = (int)dataReader[0];
}
dataReader.Close(); //关闭 DataReader 对象
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); //关闭数据库连接
}
return starId;
}
//血型ID
private int bloodtype()
{
int bloodid = 0;
string sql = string.Format("select id from bloodtype where bloodtype ='{0}' ", cboBloodType.Text);
try
{
//定义 Command 对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open(); //打开数据库连接
SqlDataReader dataReader = command.ExecuteReader(); //执行查询
//读取班级 Id
if (dataReader.Read())
{
bloodid = (int)dataReader[0];
}
dataReader.Close(); //关闭 DataReader 对象
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
return bloodid;
}
//验证密码
private bool input()
{
if (txtNewPwd.Text != txtNewRePwd.Text)
{
MessageBox.Show("您修改后的密码不一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//清空
txtNewPwd.Text = "";
txtNewRePwd.Text = "";
return false;
}
else
{
return true;
}
}
//确定
private void btnConfirm_Click(object sender, EventArgs e)
{
int starId = StarId();
int bloodid = bloodtype();
if (input())
{
string sql = "";
if (txtNewPwd.Text != "" && txtNewRePwd.Text != "")
{
sql = string.Format("update users set nickname='{0}',sex='{1}',name='{2}',starid='{3}',age='{4}',bloodtypeid='{5}',loginpwd='{6}',faceid ={7} where id={8} ", txtUserNickName.Text, cboSex.Text, txtName.Text, starId, txtAge.Text, bloodid, txtNewPwd.Text,UserHeper.pictureid, UserHeper.loginId);
}
else
{
sql = string.Format("update users set nickname='{0}',sex='{1}',name='{2}',starid='{3}',age='{4}',bloodtypeid='{5}',faceid ={6} where id={7} ", txtUserNickName.Text, cboSex.Text, txtName.Text, starId, txtAge.Text, bloodid,UserHeper.pictureid, UserHeper.loginId);
}
int result = 0;
try
{
SqlCommand com = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
result = com.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
if (result < 1)
{
MessageBox.Show("添加修改失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("服务器已接受你的请求!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (Ivface.Images.Count != 0)
{
this.picture.Image = Ivface.Images[UserHeper.id];
}
this.Close();
}
}
}
private void pnlBaseInfo_Paint(object sender, PaintEventArgs e)
{
}
}
}