如何在不同的 table 中写入我的 FK 值? C# | SQLite
How do I write my FK value in a different table? C# | SQLite
我正在做一个学校项目,我正在创建一个客户管理系统。由于要求,我需要将我的数据库架构从使用 1 table 更改为使用 2。目前,我可以写入数据库中的两个 table,但是,我遇到了麻烦获得外键的概念。我的 SQL 经验非常有限。我当前的执行语句正在写入我的两个 table,但 MedicalInformation table 中的客户端 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);
这是 tables
的一部分
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)
);
我的客户信息class
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
密钥是由 SQLite 自动生成的,因此您需要以某种方式检索它,然后再插入 MedicalInformation
table.
我没有使用 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);
只需确保 client.MedicalInfo.Client_ID
属性 存在于您的 DTO class。
我正在做一个学校项目,我正在创建一个客户管理系统。由于要求,我需要将我的数据库架构从使用 1 table 更改为使用 2。目前,我可以写入数据库中的两个 table,但是,我遇到了麻烦获得外键的概念。我的 SQL 经验非常有限。我当前的执行语句正在写入我的两个 table,但 MedicalInformation table 中的客户端 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);
这是 tables
的一部分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)
);
我的客户信息class
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
密钥是由 SQLite 自动生成的,因此您需要以某种方式检索它,然后再插入 MedicalInformation
table.
我没有使用 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);
只需确保 client.MedicalInfo.Client_ID
属性 存在于您的 DTO class。