if (textBox5.Text.Trim() == string.Empty || textBox4.Text.Trim() == string.Empty)
{
MessageBox.Show("请输入课程号和课程名!");
}
else {
string consqlserver = "server = .;Initial Catalog=Students;integrated security=true";
SqlConnection conn = new SqlConnection(consqlserver);
string sql = "insert into 课程(课程号,课程名) values('" + textBox5.Text.Trim() + "','" + textBox4.Text.Trim() + "')";
conn.Open();
string sql1 = "select * from 课程 where 课程名 ='" + textBox4.Text + "' or 课程号 ='" + textBox5.Text + "'";
string sql2 = "update 课程 set " +
" 课程号 = '" + textBox5.Text + "'," +
" 课程名 = '" + textBox4.Text + "', where 课程名 = '" + textBox4.Text + "' or 课程号 = '" + textBox5.Text + "'";
SqlCommand mysqlcommand = conn.CreateCommand();
mysqlcommand.CommandText = sql;
SqlDataReader myread = mysqlcommand.ExecuteReader();
if (myread.HasRows)
{
DialogResult result = MessageBox.Show("已存在课程,是否在原有信息上面更新信息?","Attention!",MessageBoxButtons.OKCancel, MessageBoxIcon.Information,MessageBoxDefaultButton.Button3);
switch (result)
{
case DialogResult.OK:
conn.Close();
conn.Open();
mysqlcommand.CommandText = sql2;
mysqlcommand.ExecuteNonQuery();
conn.Close();
MessageBox.Show("已完成课程的更新!");
break;
case DialogResult.Cancel:
MessageBox.Show("未做任何修改!");
break;
}
}
else
{
conn.Close();
conn.Open();
mysqlcommand.CommandText = sql;
mysqlcommand.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加该新课程成功!");
}
}
this.Hide();
Frmindex frmindex = new Frmindex();
frmindex.ShowDialog();
}