use*_*206 2 .net c# sql linq linq-to-sql
我这里有Linq-to-SQL代码,它将数据提交到各自表中的数据库中,
PatientInformation并且ResponsibleParty:
public void addPatientInformation() {
using(DbClassesDataContext myDb = new DbClassesDataContext(dbPath)){
PatientInfo patientInfo = new PatientInfo();
patientInfo.Phy_ID = physcianID;
patientInfo.Pat_First_Name = txtFirstName.Text;
patientInfo.Pat_Middle_Name = txtMiddleName.Text;
patientInfo.Pat_Last_Name = txtLastName.Text;
patientInfo.Pat_Gender = cmbGender.Text;
patientInfo.Pat_Marital_Status = cmbMaritalStatus.Text;
patientInfo.Pat_Date_Of_Birth = dtpDOB.Value;
patientInfo.Pat_Home_Add = txtHomeAdd.Text;
patientInfo.Pat_Home_Num = txtPhone.Text;
patientInfo.Pat_Work_Add = txtWorkAdd.Text;
patientInfo.Pat_Work_Num = txtWorkPhone.Text;
patientInfo.Pat_Prim_Physician = txtPrimPhysician.Text;
patientInfo.Pat_Ref_Physician = txtRefePhysician.Text;
myDb.PatientInfos.InsertOnSubmit(patientInfo);
myDb.SubmitChanges();
}
}
public void addResponsiblePartyInformation() {
using(DbClassesDataContext myDb = new DbClassesDataContext(dbPath)){
ResponsibleParty responsibleParty = new ResponsibleParty();
responsibleParty.Res_First_Name = txtResFirstName.Text;
responsibleParty.Res_Middle_Init = txtResMiddleName.Text;
responsibleParty.Res_Last_Name = txtResLName.Text;
responsibleParty.Res_Gender = cmbResGender.Text;
responsibleParty.Res_Marital_Status = cmbResMaritalStatus.Text;
responsibleParty.Res_Date_Of_Birth = dtpResDOB.Value;
responsibleParty.Res_Home_Add = txtResHomeAdd.Text;
responsibleParty.Res_Home_Num = txtResPhone.Text;
responsibleParty.Res_Work_Add = txtResWorkAdd.Text;
responsibleParty.Res_Work_Num = txtResWorkPhone.Text;
myDb.ResponsibleParties.InsertOnSubmit(responsibleParty);
myDb.SubmitChanges();
}
Run Code Online (Sandbox Code Playgroud)
并且一个名为的方法
public void submitInformationToDatabase() {
addPatientInformation();
addResponsiblePartyInformation();
MessageBox.Show("Patient Demographics Has Been added.");
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以立刻提交?
两种选择:
SubmitChanges()每次调用(也许使其成为可选)对于第二个,TransactionScope可以用来做这个而不必更改两个方法:
using(var tran = new TransactionScope()) {
method1(...);
method2(...);
tran.Complete();
}
Run Code Online (Sandbox Code Playgroud)
或者使用其他方法(添加可选参数后):
using(var ctx = new SomeDataContext(...)) {
method1(ctx, ..., submitChanges: false);
method2(ctx, ..., submitChanges: false);
ctx.SubmitChanges();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |