c# - 如何在不同的表中写入我的 FK 值? C# | SQLite

标签 c# winforms sqlite dapper

我正在开展一个学校项目,其中我正在创建一个客户管理系统。由于一项要求,我需要将数据库模式从使用 1 个表更改为使用 2 个表。目前,我可以写入数据库中的两个表,但是,我在获取外键的概念时遇到了困难。我的 SQL 经验非常有限。我当前的执行语句正在写入除 MedicalInformation 表中的客户端 ID 列之外的两个表。它显示为空,我不确定应该如何从这里继续。

cnn.Execute("INSERT INTO ClientInformation (FirstName, MiddleInitial, LastName, Phone, Email, Address, City, State, Zip, DateOfBirth, Occupation, Employer, EmergencyContact, EmergencyContactRelationship, EmergencyContactPhone) " +
            "VALUES (@FirstName, @MiddleInitial, @LastName, @Phone, @Email, @Address, @City, @State, @Zip, @DateOfBirth, @Occupation, @Employer, @EmergencyContact, @EmergencyContactRelationShip, @EmergencyContactPhone)", client);


cnn.Execute("INSERT INTO MedicalInformation (CurrentMedications, ChronicPain, ChronicPainWhere, OrthopedicPain, OrthopedicPainWhere, Pregnant, PreferedPressureLight, PreferedPressureMedium, PreferedPressureDeep, Allergies, AllergiesWhat, Cancer, HeadacheMigraine, Arthritis, Diabetes, JointReplacement, HighLowBloodPressure, Neuropathy, Fibromyalgia, Stroke, HeartAttack, KidneyDysfunction, BloodClots, Numbness, SprainsStrains, AreasOfDiscomfort) " +
            "VALUES (@CurrentMedication, @ChronicPain, @ChronicPainWhere, @OrthopedicPain, @OrthopedicPainWhere, @Pregnant, @PeferredPressureLight, @PeferredPressureMedium, @PeferredPressureDeep, @Allergies, @AllergiesWhat, @Cancer, @HeadacheMigraine, @Arthritis, @Diabetes, @JointReplacement, @HighLowBloodPressure, @Neuropathy, @Fibromyalgia, @Stroke, @HeartAttack, @KidneyDysfunction, @BloodClots, @Numbness, @SprainsStrains, @AreasOfDiscomfort) ", client.MedicalInfo);

这是表格的一部分

CREATE TABLE "MedicalInformation" (
    "_ID"   INTEGER NOT NULL UNIQUE,
    "Client_ID" INTEGER,
    
    PRIMARY KEY("_ID" AUTOINCREMENT),
    FOREIGN KEY("Client_ID") REFERENCES "ClientInformation"("Client_Id")
);

CREATE TABLE "ClientInformation" (
    "Client_Id" INTEGER NOT NULL UNIQUE,
   
    PRIMARY KEY("Client_Id" AUTOINCREMENT)
);

我的 clientinfo 类

public class ClientInformation
    {
        //Client Information
        public int Client_ID { get; set; }
        public string FirstName { get; set; }
        public string MiddleInitial { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string DateOfBirth { get; set; }
        public string Occupation { get; set; }
        public string Employer { get; set; }
        public string EmergencyContact { get; set; }
        public string EmergencyContactRelationship { get; set; }
        public string EmergencyContactPhone { get; set; }
        public MedicalInformation MedicalInfo { get; set; }
        public ClientInformation(string firstname, string middleinitial, string lastname, string phone, string email, string address, string city, string state, string zip, string dateofbirth, string occupation, string employer,
        string emergencycontact, string emergencycontactrelationship, string emergencycontactphone, MedicalInformation clientMedicainfo)
        {
            this.FirstName = firstname;
            this.MiddleInitial = middleinitial;
            this.LastName = lastname;
            this.Phone = phone;
            this.Email = email;
            this.Address = address;
            this.City = city;
            this.State = state;
            this.Zip = zip;
            this.DateOfBirth = dateofbirth;
            this.Occupation = occupation;
            this.Employer = employer;
            this.EmergencyContact = emergencycontact;
            this.EmergencyContactRelationship = emergencycontactrelationship;
            this.EmergencyContactPhone = emergencycontactphone;
            this.MedicalInfo = clientMedicainfo;
        }

    }

最佳答案

由于您的 ClientInformation.Client_Id key 是由 SQLite 自动生成的,因此您需要在插入 MedicalInformation 表之前以某种方式检索它。

我对 SQLite 没有经验,但快速浏览了一下 SQLite documentation ,我发现您可以使用 last_insert_rowid() 函数来检索最后一行插入的 ID。

您将需要在 client.MedicalInfo 对象中使用此 Client_ID 值以便稍后处理,因此我认为可以执行以下操作:

client.MedicalInfo.Client_ID = cnn.ExecuteScalar<int>("INSERT INTO ClientInformation (FirstName, MiddleInitial, LastName, Phone, Email, Address, City, State, Zip, DateOfBirth, Occupation, Employer, EmergencyContact, EmergencyContactRelationship, EmergencyContactPhone) " +
            "VALUES (@FirstName, @MiddleInitial, @LastName, @Phone, @Email, @Address, @City, @State, @Zip, @DateOfBirth, @Occupation, @Employer, @EmergencyContact, @EmergencyContactRelationShip, @EmergencyContactPhone);" +
            "SELECT last_insert_rowid();", client);

cnn.Execute("INSERT INTO MedicalInformation (Client_ID, CurrentMedications, ChronicPain, ChronicPainWhere, OrthopedicPain, OrthopedicPainWhere, Pregnant, PreferedPressureLight, PreferedPressureMedium, PreferedPressureDeep, Allergies, AllergiesWhat, Cancer, HeadacheMigraine, Arthritis, Diabetes, JointReplacement, HighLowBloodPressure, Neuropathy, Fibromyalgia, Stroke, HeartAttack, KidneyDysfunction, BloodClots, Numbness, SprainsStrains, AreasOfDiscomfort) " +
            "VALUES (@Client_ID, @CurrentMedication, @ChronicPain, @ChronicPainWhere, @OrthopedicPain, @OrthopedicPainWhere, @Pregnant, @PeferredPressureLight, @PeferredPressureMedium, @PeferredPressureDeep, @Allergies, @AllergiesWhat, @Cancer, @HeadacheMigraine, @Arthritis, @Diabetes, @JointReplacement, @HighLowBloodPressure, @Neuropathy, @Fibromyalgia, @Stroke, @HeartAttack, @KidneyDysfunction, @BloodClots, @Numbness, @SprainsStrains, @AreasOfDiscomfort); ", client.MedicalInfo);

只需确保 DTO 类中存在 client.MedicalInfo.Client_ID 属性即可。

关于c# - 如何在不同的表中写入我的 FK 值? C# | SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64703639/

相关文章:

c# - 通用处理程序中的 session ?

c# - 复选框组框不确定的复选框

python - 在 python sqlite3 模块中导入数据文件(如.csv)的任何其他方式? [不一一插入]

javascript - Phonegap 保存超过 5mb 限制的数据的最佳实践

c# - 如何从分组但仍按标题顺序返回表中的数据?

c# - 为什么相同的代码大小会产生不同大小的exe文件

windows - 允许在资源管理器样式的 ListView 中选择从第一列开始

.net - 对 Windows 窗体应用程序进行模糊测试有什么好的工具或技巧吗?

ruby-on-rails - 如何检查数据库

C#字符串不包含可能吗?