CREATETABLE Department(DepartmentID INTPRIMARYKEYIDENTITY(1,1),Name NVARCHAR(50)NOTNULL);SELECT*FROM Department
-- 插入部门数据INSERTINTO Department (Name)VALUES('人力资源部');INSERTINTO Department (Name)VALUES('技术部');INSERTINTO Department (Name)VALUES('市场部');INSERTINTO Department (Name)VALUES('销售部');CREATETABLE Employees_Lists(EmployeeID INTPRIMARYKEYIDENTITY(1,1),Name NVARCHAR(50)NOTNULL,DepartmentID INT,FOREIGNKEY(DepartmentID)REFERENCES Department(DepartmentID));-- 插入员工数据-- 假设`人力资源部`的DepartmentID是1INSERTINTO Employees_Lists (Name, DepartmentID)VALUES('张三',1);INSERTINTO Employees_Lists (Name, DepartmentID)VALUES('李四',1);-- 假设`技术部`的DepartmentID是2INSERTINTO Employees_Lists (Name, DepartmentID)VALUES('王五',2);INSERTINTO Employees_Lists (Name, DepartmentID)VALUES('赵六',2);-- 假设`市场部`的DepartmentID是3INSERTINTO Employees_Lists (Name, DepartmentID)VALUES('孙七',3);-- 假设`财务部`的DepartmentID是4INSERTINTO Employees_Lists (Name, DepartmentID)VALUES('周八',4);-- 查询获取数据SELECT a.Name AS 员工,b.Name AS 部门 FROM Employees_Lists a RIGHTJOIN Department b
ON a.DepartmentID = b.DepartmentID
C#代码实现
添加部门窗口
usingSystem;usingSystem.Data.SqlClient;usingSystem.Windows.Forms;namespaceWindowsFormsApp1{publicpartialclass 添加部门 : Form{// 数据库连接字符串privatestring connectionString ="server=172.0.0.1;uid=sa1;pwd=xyz@x123456;database=test";// 操作模式枚举enumMode{Add,Edit}// 当前操作模式privateMode currentMode;privatestring currentDepartmentName;// 仅在编辑模式下使用public 添加部门(){InitializeComponent();ConfigureForm(Mode.Add,null);}// 修改部门的构造函数public 添加部门(string departmentName){InitializeComponent();ConfigureForm(Mode.Edit, departmentName);}privatevoidConfigureForm(Mode mode,string departmentName){this.MaximizeBox =false;this.FormBorderStyle = FormBorderStyle.FixedSingle;// 禁止调整窗体大小currentMode = mode;currentDepartmentName = departmentName;if(mode == Mode.Add){this.Text ="添加部门";}elseif(mode == Mode.Edit){this.Text ="修改部门 - "+ departmentName;departmentNameTextBox.Text = departmentName;btn_adddepartmentName.Text ="修改";}}privatevoidbtn_adddepartmentName_Click(object sender,EventArgs e){string departmentName = departmentNameTextBox.Text.Trim();if(!string.IsNullOrEmpty(departmentName)){using(SqlConnection conn =newSqlConnection(connectionString)){// 检查部门是否存在string checkQuery ="SELECT COUNT(*) FROM Department WHERE Name = @Name";using(SqlCommand checkCmd =newSqlCommand(checkQuery, conn)){checkCmd.Parameters.AddWithValue("@Name", departmentName);conn.Open();int count =(int)checkCmd.ExecuteScalar();if(count >0&& currentMode == Mode.Add){MessageBox.Show("该部门已存在!"+ departmentName);}else{if(currentMode == Mode.Add){// 添加新部门string insertQuery ="INSERT INTO Department (Name) VALUES (@Name)";using(SqlCommand insertCmd =newSqlCommand(insertQuery, conn)){insertCmd.Parameters.AddWithValue("@Name", departmentName);insertCmd.ExecuteNonQuery();MessageBox.Show("部门添加成功!");}}elseif(currentMode == Mode.Edit){// 修改部门string updateQuery ="UPDATE Department SET Name = @Name WHERE Name = @OldName";using(SqlCommand updateCmd =newSqlCommand(updateQuery, conn)){updateCmd.Parameters.AddWithValue("@Name", departmentName);updateCmd.Parameters.AddWithValue("@OldName", currentDepartmentName);updateCmd.ExecuteNonQuery();//Console.WriteLine("执行修改部门SQL语句脚本:" + updateQuery);MessageBox.Show("部门修改成功!");}}}conn.Close();}}}else{MessageBox.Show("请输入部门名称!");}}}}