搞定了,谢谢了
//增加
private void btnAdd_Click(object sender, EventArgs e)
{
//信息不能为空
if (txtID.Text.Trim() == "" || txtName.Text.Trim() == ""||dtpTime .Text .Trim ()=="")
{
MessageBox.Show("信息没有填完整!","警告",MessageBoxButtons .OK ,MessageBoxIcon.Warning );
return;
}
//创建一个学生对象
Student m_Stu = new Student();
//把输入的内容赋给学生
m_Stu.StuID = txtID.Text;
m_Stu.StuName = txtName.Text;
if (radMale.Checked)
{
m_Stu.StuSex = "男";
}
else
{
m_Stu.StuSex = "女";
}
m_Stu.StuAge = dtpTime.Value.ToShortDateString ();
if (MessageBox.Show("是否增加学员信息?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
SqlConnection connection = new SqlConnection();
try
{
string strAdd = "insert StuInfo values('" + m_Stu.StuID + "','" + m_Stu.StuName + "','" + m_Stu.StuSex + "','" + m_Stu.StuAge + "')";
connection.ConnectionString = "server=4F141ACC49A245D;uid=sa;pwd=sa;database=Students";
connection.Open();
SqlCommand command = new SqlCommand(strAdd, connection);
command.ExecuteNonQuery();
//调用增加学员的方法
StuList.AddStu(m_Stu);
//把学员信息存放在一个数组中,并增加到listview的行中
string[] strValue = new string[4];
strValue[0] = m_Stu.StuID;
strValue[1] = m_Stu.StuName;
strValue[2] = m_Stu.StuSex;
strValue[3] = m_Stu.StuAge;
m_ViewItem = new ListViewItem(strValue);
lsvStuInfo.Items.Add(m_ViewItem);
}
catch (SqlException ex)
{
MessageBox.Show("学号已存在!");
}
finally
{
connection.Close();
}
}
}