您的位置:首页 > 其它

1234567890

2016-12-06 16:56 92 查看
private void Form3_Load(object sender, EventArgs e)
{
BinGrade();
}
public bool BinGrade() {

MySchool.DBHelper s = new MySchool.DBHelper();
try
{
string sql = "SELECT * FROM [MySchool].[dbo].[Grade]";
s.OpenConnection();
SqlCommand command = new SqlCommand(sql, s.Connection);
SqlDataReader myReader = command.ExecuteReader();
while (myReader.Read())
{
string gradeName = myReader["GradeName"].ToString();
comboBox1.Items.Add(gradeName);
//comboBox1.Items.Add(gradeName);
}
myReader.Close();
return true;
}
catch (Exception e)
{

MessageBox.Show(e.Message,"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
finally {
s.CloseConnection();
}

}
public bool InsertStudent() {
bool success = false;
string pwd = this.textBox2.Text.Trim();
string name=this.textBox4.Text.Trim();
int gender = 0;
if (this.rbtnFemale.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 email = this.textBox7.Text.Trim();
string birthday = string.Format("{0}-{1}-{2}", date.Year, date.Month, date.Day);
MySchool.DBHelper s = new MySchool.DBHelper();
try
{

string sqlName = String.Format("select gradeId from grade where gradeName='{0}'", grade);
SqlCommand command = new SqlCommand(sqlName, s.Connection);
s.OpenConnection();
int gradeId = (int)command.ExecuteScalar();
StringBuilder sql = new StringBuilder();
sql.AppendLine("INNERT INTO [Student] ([LoginPwd],[StudentName],[Gender],[GradeId],[Phone],[Address],[Birthday] ,[Email]) ");
sql.AppendLine("VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", pwd, name, genderId, gradeId, phone, address, birthday, email);
command = new SqlCommand(sql.ToString, s.Connection);
s.OpenConnection();
int result = command.ExecuteNonQuery();
if (result == 1)
{
string sqlNo = "SELECT @@IDENTITY FROM [Student]";
command.CommandText = sqlNo;
int studentNo = Convert.ToInt32(command.ExecuteScalar());
this.textBox1.Text = studentNo.ToString();

4000
success = true;
}

}
catch (Exception)
{

success = false;
}
finally {
s.CloseConnection();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: