问题描述
我有两个表,我想从一个表中获取数据到另一个表,实际上有两个表,
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
推荐答案
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.
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的数据中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!