如何更新具有相同列名的两个表数据..? [英] How Can I Update Two Tables Data With Same Column Name..?

查看:64
本文介绍了如何更新具有相同列名的两个表数据..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何正文可以帮助我在下面的代码中,当我在sql数据库中更新Dept

表的Dept_Name列时,它也会更新员工中相同列Dept_Name的记录

table。



以下是部门表的更新代码: -



Can any body Help me in below code in which when i update Dept_Name column of Dept
table in sql database it will also update the record of same column Dept_Name in Staffs
table.

Below is the Update Code for Dept Table:-

Dim obj As New ConStr
            Dim sql As String
            sql = "update Dept set Dept_Name='" & TextBox2.Text & "' where Dept_No=" & TextBox1.Text
            obj.RunInsertDeleteUpdateQry(sql)
            'RefreshGrid()
            TextBox1.Clear()
            TextBox2.Clear()
            MsgBox("Data Updated Successfully")





以下是员工表的更新代码: -





Below is the Update Code for Staffs Table:-

Dim obj As New ConStr
            Dim sql As String
            sql = "update Staffs set Dept_Name='" & TextBox2.Text & "' where Dept_No=" & TextBox1.Text
            obj.RunInsertDeleteUpdateQry(sql)
            'RefreshGrid()
            TextBox1.Clear()
            TextBox2.Clear()
            MsgBox("Data Updated Successfully")





但员工表未更新。



请帮帮我.........



But Staffs table was not updated.

Please help me.........

推荐答案

检查 staffs 表是否包含具有此部门编号和名称的数据。



另外,您应该使用命令参数来避免 SQL注入 [ ^ ]。
Check whether staffs table contains data with this dept number and name.

As an aside, you should use command parameters to avoid SQL Injection[^].


我建​​议不要重复几个表上的数据。如果员工链接到某个部门,请定义从员工到部门的外键关系,并且仅在部门表中具有部门名称。在获取数据时,加入部门和工作人员以获取两个表的数据。



但是,如果您需要更新两个表,一个非常传统的方法是创建一个包含更新的存储过程。存储过程将输入值作为参数。



看看创建程序 [ ^ ]
I would suggest not to repeat the data on several tables. If the staff is linked to a department, define a foreign key relation from Staff to Department and have the department name only in the department table. When fetching the data, join both department and staff in order to get data rom both tables.

However, if you need to update both tables, a very traditional approach is to create a stored procedure which would contain the updates. The stored procedure would have the input values as parameters.

Have a look at CREATE PROCEDURE[^]


只是添加其他人所说的内容,不要那样做!

不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
Just to add to what the others have said, don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于如何更新具有相同列名的两个表数据..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆