transaction sql max ID + C#
HejSå vender man tilbage til det.
Jeg sidder og skal indsætte i 2 forskellige tabeller gerne på 1 gang.
Koden
public Sample insertSampleWithproject(string sample_EksternalNo, DateTime sample_Date, string materiale, string diagnose,
int patient_ID, int placement_ID, int location_ID, int sampleType_ID,int Project_ID)
{
var newSample = new Sample();
var newPatient = new Patient();
var newPlacement = new Placement();
var newLocation = new Location();
var newSampleType = new SampleType();
var newProject = new Project();
newSample.Patient = newPatient;
newSample.Placement = newPlacement;
newSample.Location = newLocation;
newSample.SampleType = newSampleType;
newSample.Project = newProject;
DBSample S = new DBSample();
//Sample maxID = new GetMaxSample();
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HFLDB"].ConnectionString))
{
conn.Open();
SqlCommand command = conn.CreateCommand();
SqlTransaction transaction;
transaction = conn.BeginTransaction("SampleTransaction");
command.Connection = conn;
command.Transaction = transaction;
try
{
command.CommandText = "INSERT INTO HFL_Sample(Sample_EksternalNo,Sample_Date,Materiale,Diagnose, Patient_ID, Placement_ID ,Location_ID ,SampleType_ID)" +
"VALUES (@Sample_EksternalNo,@Sample_Date,@Materiale,@Diagnose, @Patient_ID, @Placement_ID ,@Location_ID ,@SampleType_ID)";
command.Parameters.AddWithValue("@Sample_EksternalNo", sample_EksternalNo);
command.Parameters.AddWithValue("@Sample_Date", sample_Date);
command.Parameters.AddWithValue("@Materiale", materiale);
command.Parameters.AddWithValue("@Patient_ID", patient_ID);
command.Parameters.AddWithValue("@Diagnose", diagnose);
command.Parameters.AddWithValue("@Placement_ID", placement_ID);
command.Parameters.AddWithValue("@Location_ID", location_ID);
command.Parameters.AddWithValue("@SampleType_ID", sampleType_ID);
command.ExecuteNonQuery();
newSample.Patient.Patient_ID = patient_ID;
newSample.Placement.Placement_ID = placement_ID;
newSample.Location.Location_ID = location_ID;
newSample.SampleType.SampleType_ID = sampleType_ID;
//command.CommandText = "SELECT MAX(Sample_ID) FROM HFL_Sample";
S.GetMaxSample();
command.CommandText = "INSERT INTO HFL_ProjSample (Sample_ID, Project_ID) VALUES (@Sample_ID, @Project_ID)";
command.Parameters.AddWithValue("@Sample_ID", S);
command.Parameters.AddWithValue("@Project_ID", Project_ID);
command.ExecuteNonQuery();
newSample.Project.Project_ID = Project_ID;
transaction.Commit();
Console.WriteLine("Both Records where inserted!");
}
catch (Exception ex)
{
Console.WriteLine("(DBSample) Commit Exception Type: {0}", ex.GetType());
Console.WriteLine("Message : {0}", ex.Message);
//try a rollback if it throws and exception
try
{
transaction.Rollback();
}
catch (Exception rollEx)
{
Console.WriteLine("Rollback Exception Type: {0}", rollEx.GetType());
Console.WriteLine("Message: {0}", rollEx.Message);
}
}
}
return newSample;
}
den metohde jeg så kalder getMaxSample
public Sample GetMaxSample()
{
var foundID = new Sample();
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HFLDB"].ConnectionString))
{
using (var cmd = new SqlCommand("SELECT MAX(Sample_ID) FROM HFL_Sample"))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
try
{
foundID = BuildSample(reader);
}
catch (Exception ex)
{
Console.WriteLine("(DBSample) : GetMaxSample_ID() :: Fejl :: " + ex);
}
finally { }
}
}
}
}
return foundID;
}
Jeg skal til at indsætte i tabel 2 gerne bruge det ID der er autoincrementet fra tabel 1
Kan det lad sig gør?