本文介绍了在具有父子关系的两个表中重新存储和保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SQL数据库的初学者...我有两个表tblTeacher和tblTeacherAcnt。在表老师我有教师生物数据和帐号ID每个教师帐户记录每月在tblteacheracnt。
所以我的问题是如何为每位教师保存tblteacheraccount中的数据并将数据追溯回来



推荐答案

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);
            }
        }
    }
}


这篇关于在具有父子关系的两个表中重新存储和保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:11