DB db = new DB();//数据库操作的类
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();
SQL sql = new SQL();//专门存放sql语句的类
private void button1_Click(object sender, EventArgs e)//查询
{
con = db.GetCon();
con.Open();
string SelectSql = sql.selectspecialty();
SqlDataAdapter dap = new SqlDataAdapter(SelectSql, con);
dap.Fill(ds, "SpecialtyInfo");
dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
private void button3_Click(object sender, EventArgs e)//删除
{
if (dataGrid1[dataGrid1.CurrentCell] != null)
{
string sql = "select specialtyname from specialtyinfo where specialtyid=" + ds.Tables ["specialtyinfo"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + "";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (!dr.Read())//前进到下一条记录
{
MessageBox.Show("删除专业'" + ds.Tables["specialtyinfo"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim() + "'", "提示");
dr.Close();
}
else
{
dr.Close();
sql = "delete specialtyinfo where specialtyid=" + ds.Tables["specialtyinfo"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("删除专业'" + ds.Tables["specialtyinfo"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim() + "'成功", "提示");
}
}
在button3_Click后,datagrid中如何进行刷新呢?button1_Click是返回specialtyinfo中所有的信息