在具有父子关系的两个表中重新存储和保存数据? [英] Retriving and saving Data In two Table having Parent Child Relation ?
本文介绍了在具有父子关系的两个表中重新存储和保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是SQL数据库的初学者...我有两个表tblTeacher和tblTeacherAcnt。在表老师我有教师生物数据和帐号ID每个教师帐户记录每月在tblteacheracnt。
所以我的问题是如何为每位教师保存tblteacheraccount中的数据并将数据追溯回来
teacherid是主要关键所在外键
推荐答案
你有几个选择:
1-你可以手动完成。也就是说,您可以自己编写所有DML代码。
2-您可以使用o / r映射器,或者为您生成代码的其他工具。
例如,你可以使用linq来sql。创建一个datacontext,使用linq操作它。但是既然你是初学者,我建议你弄脏你的手,手动编码所有东西。
you have several options:
1- you can do it manually. ie, you can write all DML codes yourself.
2- you can use a o/r mapper, or some other tool that it produces codes for you.
for example, you can use linq to sql. create a datacontext, manipulate it by using linq. but since you beginner, i advice you dirt your hands, code everything manually.
create table TEACHER (
ID int identity(1,1) primary key,
NAME nvarchar(10)
)
create table TEACHER_ACC (
ID int identity(1,1) primary key,
TEACHER_ID int not null,
foreign key(TEACHER_ID) references TEACHER(ID)
)
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_DML
{
class Program
{
static void Main(string[] args)
{
try
{
string connstr = @"data source=.\sqlexpress; initial catalog=TEST; integrated security=true";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
// insert teacher
string cmdstr = @"insert into TEACHER(NAME) values ('teacher1')";
SqlCommand cmd;
using (cmd = new SqlCommand(cmdstr, conn))
{
Console.WriteLine(cmd.ExecuteNonQuery() + " inserted");
}
// insert teacher_acc
cmdstr = @"insert into TEACHER_ACC(TEACHER_ID) values (1)";
using (cmd = new SqlCommand(cmdstr, conn))
{
Console.WriteLine(cmd.ExecuteNonQuery() + " inserted");
}
string selstr = @"select ID, TEACHER_ID from TEACHER_ACC";
using (cmd = new SqlCommand(selstr, conn))
{
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
Console.WriteLine(string.Format("{0} {1}", r[0], r[1]));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
这篇关于在具有父子关系的两个表中重新存储和保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文