public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void 编辑学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.BindGrade();
f.MdiParent = this;
f.Show();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool BindGrade()
{
DBHelper db = new DBHelper();
try
{
string sql = "select * from grade";
db.OpenConnection();
SqlCommand comm = new SqlCommand(sql, db.Connection);
SqlDataReader reader = comm.ExecuteReader();
nianji.Items.Add("请选择");
nianji.SelectedIndex =0;
while (reader.Read())
{
string gradeName = reader["gradename"].ToString();
nianji.Items.Add(gradeName);
}
reader.Close();
return true;
}
catch (Exception )
{
MessageBox.Show("系统发生错误!", "调试", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
finally {
db.CloseConnection();
}
}
private void nianji_SelectedIndexChanged(object sender, EventArgs e)
{
}
public bool CheckInput() {
if(mima.Text.Trim().Equals(string.Empty)){
MessageBox.Show("请输入密码","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.mima.Focus();
return false;
}else if(xingming.Text.Trim().Equals(string.Empty)){
MessageBox.Show("请输入姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.xingming.Focus();
return false;
}else if(nianji.Text.Trim().Equals(string.Empty)){
MessageBox.Show("请选择年级", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.nianji.Focus();
return false;
}
else if (email.Text.Trim().Equals(string.Empty))
{
int i = this.email.Text.IndexOf('@');
if (i == -1)
{
MessageBox.Show("请输入正确邮箱", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.email.Focus();
return false;
}
else {
return true;
}
}
else {
return true;
}
}
private void baocun_Click(object sender, EventArgs e)
{
if (CheckInput() && InsertStudent())
{
MessageBox.Show("登陆成功","提示",MessageBoxButtons.OK,MessageBoxIcon.None);
}
}
public bool InsertStudent() {
bool success = false;
string phone = this.Text.Trim();
int gender =0;
if (this.nan.Checked)
{
gender =1;
}
string pwd = this.mima.Text.Trim();
string gendername =this.nianji.Text.Trim();
string address = this.dizhi.Text.Trim();
string name = this.xingming.Text.Trim();
DBHelper db = new DBHelper();
DateTime date = this.chushengriqi.Value;
string br = string.Format("{0}-{1}-{2}",date.Year,date.Month,date.Day);
try
{
string sqlName = string.Format("select gradeid from grade where gradeName='{0}'", gendername);
SqlCommand comm = new SqlCommand(sqlName, db.Connection);
db.OpenConnection();
int gradeId = (int)comm.ExecuteScalar();
StringBuilder sql = new StringBuilder();
sql.AppendLine(" insert into student (loginpwd,studentname,gender,gradeid ,phone,address ,birthday ,email) ");
sql.AppendFormat(" values('{0}','{1}',{2},{3},'{4}','{5}','{6}','{7}')", pwd, name,gender, gradeId, phone, address, br, email);
comm = new SqlCommand(sql.ToString(), db.Connection);
db.OpenConnection();
int result = comm.ExecuteNonQuery();
if (result == 1)
{
string sqlNo = "select @@IDENTITY FROM student";
comm.CommandText = sqlNo;
int studentNo = Convert.ToInt32(comm.ExecuteScalar());
this.xuehao.Text = studentNo.ToString();
success = true;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
success = false;
}
finally {
db.CloseConnection();
}
return success;
}