如何从其他表中获取数据并存储在其他表中? [英] How can I get the data from other table and store in other table?

查看:75
本文介绍了如何从其他表中获取数据并存储在其他表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一张桌子



 创建  TABLE  [dbo]。[部门](
[Dept_ID] INT IDENTITY 10 10 NOT NULL
[Dept_Name] VARCHAR 50 NOT NULL
CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([Dept_ID] ASC ),
CONSTRAINT [FK_Department_Employee] < span class =code-keyword> FOREIGN KEY ([dept_ID])参考 [dbo]。[部门]([Dept_ID])
);







第二桌



<前lang =sql> CREATE TABLE [dbo]。[Employee](
[Dept_ID] INT NOT NULL
[Dept_Name] VARCHAR 50 NOT NULL
[Emp_ID] INT IDENTITY 1 1 NOT NULL
[Emp_Name] VARCHAR 50 ñ OT NULL
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([Emp_ID] ASC
);







部门表格数据 -

Dept_ID ----- Dept_Name

10 ---------- IT

20 ------ ---- CS

30 ---------- EE



我想在表Employee中输入数据如果我从下拉列表中选择Dept_Name并输入Emp_Name,它可以像Emp_Name,Emp_ID,Dept_Name和Dept_ID一样保存;和dept_ID将从下拉列表项中的相应Dept_Name。

解决方案

在员工表上运行插入查询 -

 字符串 query =   INSERT INTO dbo。 employee(dept_name,emp_name)VALUES(@denamename,@ employee_name); 

command.Parameters.AddWithValue( @ deptname,dropdown1.value )
command.Parameters.AddWithValue( @ employee_name,txtEmpName.value)

command.ExecuteNonQuery();


首先,我建议不要在employee表中保存部门名称。您已经在表之间建立了关系(Dept_ID),因此您始终可以使用连接来获取引用的部门名称。如果要将部门名称保存到员工,当部门名称更改时,您还需要更改所有相应员工行中的名称。



保存部分名称,添加到 Abhinav S's [ ^ ]回答,问题是当您将数据提取到用户界面时,你需要获取部门名称和ID。此信息可以存储在数据表,集合等中,但重要的是,当您将数据绑定到下拉列表时,您需要根据用户选择的文本值访问id。



根据您使用的技术,某些控件可以单独绑定显示成员(部门名称)和数据成员(部门ID),也可以绑定整个对象(即数据行等)对于特定的下拉项目。



保存时,您将获取所选的键值并在语句中使用它,如解决方案1中的示例,但没有部门名称,所以类似



  string  query =   INSERT INTO dbo.employee(dept_id,emp_name)VALUES(@ deptid,@ empname); 

command.Parameters.AddWithValue( @ deptid,deptid_value)
command.Parameters.AddWithValue( @ empname,empname_value)

command.ExecuteNonQuery();


  String 查询=   INSERT INTO dbo.employee(Emp_Name,Emp_ID,Dept_Name,Dept_ID)VALUES(@ Emp_Name,Dept_Name,Dept_ID) ; 

command.Parameters.AddWithValue( @ Emp_Name,txtEmpName.Text )
command.Parameters.AddWithValue( @ Dept_Id,ddldept_Id.SelectedItem.Value)或
// command.Parameters.AddWithValue(@ Dept_Id,ddldept.SelectedValue)
command.Parameters.AddWithValue( @ Dept_Name,ddldept_Name.SelectedItem.Text)
// emp_id是身份所以不需要传递
command.ExecuteNonQuery() ;
// 你必须存储,只有dept_id足以存储im emp表。通过内连接的方式你可以获得部门名称..: - )


my 1st table

CREATE TABLE [dbo].[Department] (
    [Dept_ID]   INT          IDENTITY (10, 10) NOT NULL,
    [Dept_Name] VARCHAR (50) NOT NULL,
    CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([Dept_ID] ASC),
    CONSTRAINT [FK_Department_Employee] FOREIGN KEY ([Dept_ID]) REFERENCES [dbo].[Department] ([Dept_ID])
);




2nd table

CREATE TABLE [dbo].[Employee] (
    [Dept_ID]   INT          NOT NULL,
    [Dept_Name] VARCHAR (50) NOT NULL,
    [Emp_ID]    INT          IDENTITY (1, 1) NOT NULL,
    [Emp_Name]  VARCHAR (50) NOT NULL,
    CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([Emp_ID] ASC)
);




Data of Department table-
Dept_ID-----Dept_Name
10----------IT
20----------CS
30----------EE

I want to enter data in table Employee such that if I select the Dept_Name from the dropdownlist and enter the Emp_Name, it could be save like Emp_Name, Emp_ID, Dept_Name and Dept_ID; and Dept_ID will be to the corresponding Dept_Name from dropdownlist items.

解决方案

Run an insert query on the employee table -

String query = "INSERT INTO dbo.employee (dept_name,emp_name) VALUES (@deptname, @employee_name)";

command.Parameters.AddWithValue("@deptname",dropdown1.value)
command.Parameters.AddWithValue("@employee_name",txtEmpName.value)

command.ExecuteNonQuery();


First of all I would advice not to save the department name in the employee table. You already have a relationship between the tables (Dept_ID) so you can always fetch the referenced department name by using a join. If you would save the department name to the employee, when the department name changes, you would need also to change the name in all corresponding employee rows.

Conserning the saving, to add to Abhinav S's[^] answer, the thing is that when you fetch the data into your user interface, you need to fetch both department name and id. This information could be stored inside a datatable, collection etc but the important thing is that when you bind the data to the drop down list, you need to have access to the id based on the text value the user selected.

Depending on the technology you use, some controls provide ability to bind display member (department name) and data member (department id) separately or you can bind the whole object (i.e. datarow etc) for a specific drop down item.

When saving you fetch the selected key value and use it in the statement, like in the example in solution 1, but without department name, so something like

string query = "INSERT INTO dbo.employee (dept_id,emp_name) VALUES (@deptid, @empname)";
 
command.Parameters.AddWithValue("@deptid",deptid_value)
command.Parameters.AddWithValue("@empname",empname_value)
 
command.ExecuteNonQuery();


String query = "INSERT INTO dbo.employee (Emp_Name, Emp_ID, Dept_Name,Dept_ID) VALUES (@Emp_Name,Dept_Name,Dept_ID)";

command.Parameters.AddWithValue("@Emp_Name",txtEmpName.Text)
command.Parameters.AddWithValue("@Dept_Id",ddldept_Id.SelectedItem.Value) or
//command.Parameters.AddWithValue("@Dept_Id",ddldept.SelectedValue)
command.Parameters.AddWithValue("@Dept_Name",ddldept_Name.SelectedItem.Text)
// emp_id is identity so not require to pass
command.ExecuteNonQuery();
// You have to store , only dept_id is enough to store im emp table .by means of inner join u can get the dept name..:-)


这篇关于如何从其他表中获取数据并存储在其他表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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