从一个表到另一个具有相同ID的数据中获取数据 [英] Fetching data From One Table to another with same ID

查看:235
本文介绍了从一个表到另一个具有相同ID的数据中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个表,我想从一个表中获取数据到另一个表,实际上有两个表,

ID名称ImageUrl和ID EmpID名称

我希望第一个表中具有ID的名称说应将2放置到第二个表中具有EmpID = 2的名称.

两个表通过ID和EmpID之间的FK连接的一件事
可以说
ID名称ImageUrl

2阿里Images/1.jpg
3凯利图片/2.jpg

在其他表格中

ID EmpID名称CheckInTime CheckOutTime

20 2
21 3

因此,我希望当第一个表的ID和第二个表的EmpID相同时,第二个表中的第一个表中也应该有名称.实际上,图像是通过列表绑定的,并放置在图像"按钮中.因此,当我单击图像"按钮时,它将在数据库中添加具有当前CheckInTime的条目.名称问题,我无法插入.代码在这里



I have two table I Want to fetch Data from one table to another, In fact there are two tables,

ID Name ImageUrl And ID EmpID Name

I want that Name that is in the First Table with ID say that 2 should be Placed to the Name in the second table with EmpID= 2.

One thing both tables are connected by FK between ID And EmpID
Lets say
ID Name ImageUrl

2 Ali Images/1.jpg
3 Kelly Images/2.jpg

And in other table

ID EmpID Name CheckInTime CheckOutTime

20 2
21 3

So I want that When the ID of first Table And EmpID of second table are same then there should also be name there in the second table that is Showing in first Table. In fact Images are bind by a list and place in the Image button. So when i click on the Image button it adds an entry in the DB with current CheckInTime. Problem with the Name I am not able to insert it. Code is here

public partial class Employee
    

public CheckInCheckOut LastCheckInCheckOut
        {
            get
            {
                return this.CheckInCheckOuts.Last();
            }
        }

    }

    public partial class _Default : System.Web.UI.Page
    {

        DataClasses1DataContext db = new DataClasses1DataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
             if(!IsPostBack) BindData();
        }

       public void BindData()
        {
            
                DataClasses1DataContext db = new DataClasses1DataContext();

                var data = from emp in db.Employees
                           select emp;

                var items = data.ToList();
          
                    ListView1.DataSource = data;
                    ListView1.DataBind();
        }


        protected void UpdateTime(int EmpId)
        {
           DateTime dte = DateTime.Today;
            DataClasses1DataContext db = new DataClasses1DataContext();

            var chkInOut = (CheckInCheckOut)(from r in db.CheckInCheckOuts
                                             where r.EmpID == EmpId && r.CheckInTime.Value.Date.CompareTo(dte) == 0
                                             select r).FirstOrDefault();

            if (chkInOut == null)
            {
                chkInOut = new CheckInCheckOut();
                chkInOut.EmpID = EmpId;
                 
                chkInOut.CheckInTime = DateTime.Now;
                db.CheckInCheckOuts.InsertOnSubmit(chkInOut);
                db.SubmitChanges();
            }
            else
            {
                chkInOut.CheckOutTime = DateTime.Now;
                db.SubmitChanges();
            }
 
        }
 
        protected void ImageButton_Command(object sender, CommandEventArgs e)
        {
            int EmpId = Int32.Parse(e.CommandArgument.ToString());
                UpdateTime(EmpId);
                 
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongTimeString();  
        }

    }

}{



那我可以在这里使用什么技巧.

问候

Ali



So what trick I can use here.

regards

Ali

推荐答案

没有技巧,如果在SQL语句中执行此操作,则使用 join (在这种情况下,很可能是内部联接) ).

有关教程,请参见: SQL联接 [
No trick, if you do this in your SQL statement, you use a join (most likely an inner join in this case).

For a tutorial, see: SQL Joins[^]


尝试一下
SELECT t1.ID, t1.Name, t1.ImageUrl, t2.ID, t2.EmpID, t2.Name
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.Id = t2.EmpId
WHERE t1.Id = 2


但请仔细阅读答案1.


but carefully read answer 1.


如果要使用C#编写,则可能要使用LINQ:

If you want to do it in C#, then you probably want to use LINQ:

var result = from i1 in Table1
   join i2 in Table2
   on i1.Id equals i2.EmpId
   select new
    {
      Name1=t1.Name,
      ImageUr=t1.ImageUrl,
      Id=t2.ID,
      EmpId=t2.EmpID,
      OtherName=t2.Name
    };



注意:这是一个内部联接,因此不会有不匹配记录的条目.



Note: This is an inner join, so will not have entries for records that do not match.


这篇关于从一个表到另一个具有相同ID的数据中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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