namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label10_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
public bool DBHelper() {
DBHelper db = new DBHelper();
try
{
string sql = "Select*from[Grade]";
db.OpenConnection();
SqlCommand com = new SqlCommand(sql, db.Connection);
SqlDataReader reader = com.ExecuteReader();
this.comboBox1.Items.Add("请选择");
this.comboBox1.SelectedIndex = 0;
while (reader.Read())
{
string gradename = reader["GradeName"].ToString();
comboBox1.Items.Add(gradename);
}
reader.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("系统发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
finally {
db.CloseConnection();
}
}
private void Form2_Load(object sender, EventArgs e)//调用上面的方法
{
DBHelper();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void insertStudent() {
bool success = false;
string pwd = this.textBox2.Text.Trim();//密码
string name = this.textBox4.Text.Trim();//姓名
int gender = 0;//获取性别
if (this.radioButton1.Checked) {
gender = 0;
}
else {
gender = 1;
}
int genderId = (int)gender;
string grade=this.comboBox1.Text.Trim();//年级
string phone=this.textBox5.Text.Trim();//电话
string address=this.textBox6.Text.Trim();//地址
DateTime date = this.dateTimePicker1.Value;//出生日期
string birthday=string.Format("{0}-{1}-{2}",date.Year,date.Month,date.Day);//生日
string email=this.textBox7.Text.Trim();//邮件
DBHelper db = new DBHelper();
try
{
string sqlName = String.Format("select gradeId from grade where gradeName='{0}'",grade);
SqlCommand com = new SqlCommand(sqlName,db.Connection);//创建Command对象
db.OpenConnection();
int gradeId = (int)com.ExecuteScalar();//获得年级编号
StringBuilder Sql = new StringBuilder();
Sql.AppendLine(@"insert into [Student],[StudentName],[Gender],[GradeId],[Phone],[Address],
[Birthday],[Email]");
Sql.AppendFormat("VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",pwd,name,
genderId,gradeId,phone,address,birthday,email);
com = new SqlCommand(Sql.ToString(), db.Connection);//创建Command对象
db.OpenConnection();
int result = com.ExecuteNonQuery();
if(result==1){
string SqlNo = "select @@IDENTITY from [student]";
com.CommandText = SqlNo;
int studentNo = Convert.ToInt32(com.ExecuteScalar());//学号
this.textBox1.Text = studentNo.ToString();
success = true;
}
}
catch (Exception ex)
{
success=false;
}
finally{
db.CloseConnection();
}
}
}
}
实现完整版的管理员主窗体和学生主窗体
最新推荐文章于 2020-07-08 15:57:15 发布