回复 3# 的帖子
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace 学生专业管理系统
{
public partial class BrowseSpecialty : Form
{
public BrowseSpecialty()
{
InitializeComponent();
}
OleDbConnection connection1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:\\学生专业管理.mdb");
//定义数据集对象ds用于保存查询专业信息返回的结果集
DataSet ds;
//在窗口的初始化事件中,对dataGrid1进行绑定
private void BrowseSpecialty_Load(object sender, EventArgs e)
{
connection1.Open();
string sql = "select specialtyid as
编号,specialtyname as 专业名称,remark as 专业描述 from specialtyinfo";
OleDbDataAdapter adb = new OleDbDataAdapter(sql, connection1);
ds = new DataSet();
0 ------ adb.Fill(ds, "specialty");----------
dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
//当dataGrid1中当前单元格发生变化时,将对应的专业名称显示在dataGrid1的标题中
private void dataGrid1_CrrentCellChanged(object sender, EventArgs e)
{
dataGrid1.CaptionText = dataGrid1[dataGrid1.CurrentRowIndex, 1].ToString();
}
//点击修改按钮(button1)时,调用ModifySpecialty窗体
private void button1_Click(object sender, EventArgs e)
{
ModifySpecialty modifySpecialty;
if (dataGrid1[dataGrid1.CurrentCell] != null)
{
modifySpecialty = new ModifySpecialty();
modifySpecialty.textBox1.Text = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][0].ToString().Trim();
//获得专业编号
modifySpecialty.textBox2.Text = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][1].ToString().Trim();
//获得专业名称
modifySpecialty.richTextBox1.Text = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][2].ToString().Trim();
//获得专业描述
modifySpecialty.ShowDialog();
//生成新窗体
}
}
//删除按钮事件
private void button2_Click(object sender, EventArgs e)
{
if (dataGrid1[dataGrid1.CurrentCell] != null)
{
string sql = "select specialtyname from specialtyinfo where specialtyid=" + ds.Tables["specialty"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + "and specialtyid not in (select distinct specialtyinfo.specialtyid from classinfo inner join specialtyinfo on classinfo.specialtyname=specialtyinfo.specialtyname)";
OleDbCommand cmd = new OleDbCommand(sql, connection1);
OleDbDataReader dr;
dr = cmd.ExecuteReader();
if (!dr.Read())
{
MessageBox.Show("删除专业'" + ds.Tables["specialty"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim() + "'失败,请先删除与此专业相关的班级", "提示");
dr.Close();
}
else
{
dr.Close();
sql = "delete * from specialtyinfo where specialtyname not in (select distinct specialtyname from classinfo) and specialtyid=" + ds.Tables["specialty"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim();
= sql;
cmd.ExecuteNonQuery();
MessageBox.Show("删除专业'" + ds.Tables["specialty"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim() + "'成功", "提示");
}
}
}
//退出
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
dataGrid1.CaptionText = dataGrid1[dataGrid1.CurrentRowIndex, 1].ToString();
}
}
}