C# 使用Linq to SQL将数据同时插入到不同的表中

C# 使用Linq to SQL将数据同时插入到不同的表中,c#,.net,sql,linq,linq-to-sql,C#,.net,Sql,Linq,Linq To Sql,我这里有Linq到SQL代码,它将数据提交到各自表中的数据库中, 患者信息和责任方: public void addPatientInformation() { using(DbClassesDataContext myDb = new DbClassesDataContext(dbPath)){ PatientInfo patientInfo = new PatientInfo(); patientInfo.Phy_ID = physcianID;

我这里有Linq到SQL代码,它将数据提交到各自表中的数据库中,
患者信息
责任方

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();
   }
和一个名为

public void submitInformationToDatabase() {
            addPatientInformation();
            addResponsiblePartyInformation();
            MessageBox.Show("Patient Demographics Has Been added.");
        }
有什么方法可以让我立刻提交吗

有两种选择:

  • 在中传递数据上下文,不要每次调用
    SubmitChanges()
    (可能是可选的)
  • 使用事务
对于第二种情况,
TransactionScope
可以用来执行此操作,而无需更改两种方法:

using(var tran = new TransactionScope()) {
    method1(...);
    method2(...);
    tran.Complete();
}
或使用其他方法(添加可选参数后):


如果您可以将代码重构为类似以下示例的内容,那么这两条记录将插入到同一个
submit

using(DbClassesDataContext myDb = new DbClassesDataContext(dbPath)){

      myDb.PatientInfos.InsertOnSubmit(patientInfo);
      myDb.ResponsibleParties.InsertOnSubmit(responsibleParty);
      myDb.SubmitChanges();
}

将连接作为参数传递给方法和使用系统。事务:

public void submitInformationToDatabase() {
         using (System.Transactions.TransactionScope tr = new System.Transactions.TransactionScope())
           {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(dbPath);
            con.Open();
            addPatientInformation(con);
            addResponsiblePartyInformation(con);
            tr.Complete();
           }
            MessageBox.Show("Patient Demographics Has Been added.");
        }

这看起来很有趣,但到目前为止,我不知道什么是事务作用域。@user962206它是一个“环境事务”,可由多个提供程序使用。对于SQL Server,它支持LTM和DTC(“轻量级事务管理器”和“分布式事务协调器”)机制。或者用英语说:你会得到一个数据库事务,所以它是按照通常的“ACID”规则提交的。你能提供一些例子吗??我没有得到方法(cyx,…,submitChanges:false);还有更简单的吗?@user“更简单”是事务范围,如图所示。我在其他设置中的意思是:与其在这些方法中创建数据上下文,不如在外部创建数据上下文并将其作为参数传入,然后在Afterwards外部调用SubmitChanges您似乎建议使用连接级别事务,但您演示了环境事务(TransactionScope)。如果使用环境事务,则不需要对现有方法进行任何更改;在这种情况下,您不需要传入连接。这就是说,这将有助于使它更有可能使用LTM而不是DTC-这就是您试图展示的意图吗?因为两个数据上下文使用相同的连接字符串,因此无需打开2个连接并加载更多池,再加上一个连接一个“全局”事务,这样就不会发生;这里的数据上下文范围很窄,将被干净地处理(释放回池)。实际上,它变得更加有趣,因为数据上下文也可以(取决于它的设置)在不需要连接的时候释放连接-iirc这是传入连接字符串时的默认值。这并不意味着它每次都有不同的底层连接。感谢您提供的信息,但无论如何,有时我们需要设置
Pooling=false
,因为经过一个小时的艰苦测试,它已满。如果您必须关闭Pooling,您可能出了问题(可能无法可靠地释放它们?)。如果你真的需要一个更大的池,你可以增加它。我们在stackoverflow使用标准池。
public void submitInformationToDatabase() {
         using (System.Transactions.TransactionScope tr = new System.Transactions.TransactionScope())
           {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(dbPath);
            con.Open();
            addPatientInformation(con);
            addResponsiblePartyInformation(con);
            tr.Complete();
           }
            MessageBox.Show("Patient Demographics Has Been added.");
        }