您的位置:首页 > 其它

最后一本书 第六章课后练习3,4

2016-12-13 16:34 288 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 最后一本书_第六章_课后练习3__4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ss();

}
public string sql = "Data Source=.;Initial Catalog=GS;Integrated Security=True";
DataSet ds = new DataSet();
public void ss() {
string stu = "SELECT * FROM [GS].[dbo].[Employee] a,[dbo].[Department] b where a.DeptId=b.DeptId";
SqlDataAdapter da = new SqlDataAdapter(stu,sql);
//SqlConnection conn = new SqlConnection(sql);
//SqlCommand comm = new SqlCommand(stu, conn);
//da.SelectCommand=comm;
DataView dv = new DataView(ds.Tables["student"]);
try
{
da.Fill(ds,"student");
dataGridView2.DataSource =ds.Tables["student"];

}
catch (Exception e)
{

MessageBox.Show(e.Message);
}
}

private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
{
//string ss = string.Format("选中{0}节点,深度为{1}", treeView2.SelectedNode.Text, treeView2.SelectedNode.Level);
//MessageBox.Show(ss,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
hh();

}
public void hh() {
DataView dv = new DataView(ds.Tables["student"]);
if (treeView2.SelectedNode.Level == 1)
{
string deptname = treeView2.SelectedNode.Text;
dv.RowFilter = string.Format("DeptName='{0}'",  deptname);
}
dataGridView2.DataSource = dv;
}
public void sanchu(){

}

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridView2.SelectedRows.Count>0)
{
DialogResult q=MessageBox.Show("确定要删除吗?同时会删除学生的成绩!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
if(q==DialogResult.OK){
string DeptName=Convert.ToString(dataGridView2.SelectedRows[0].Cells[0].Value);
string a = string.Format("delete from Employee where DeptId='{0}';delete from Department where DeptId='{0}'", DeptName);
MessageBox.Show(a);
SqlConnection conn = new SqlConnection(sql);
try
{
conn.Open();
SqlCommand comm = new SqlCommand(a, conn);
int i = comm.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("删除成功!", "提示");
}
else
{
MessageBox.Show("删除失败!", "提示");
}

}
catch (Exception ee)
{

MessageBox.Show(ee.Message);
}
finally {
conn.Close();
}
}

}

}

private void 获得部门ToolStripMenuItem_Click(object sender, EventArgs e)
{
string s = Convert.ToString(dataGridView2.CurrentRow.Cells[0].Value);
MessageBox.Show("Id为:"+s);
}

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