Я написал транзакцию ниже в C #, чтобы вставить данные в 4 таблицы в Access.Я развернул приложение, и когда один из пользователей вставляет данные, происходит сбой вставки и транзакция не откатывается.Я могу сказать, что произошла неудачная вставка, потому что в столбце идентификаторов пропущены номера при успешной вставке.
Можете ли вы просмотреть мой код, чтобы увидеть, что может быть причиной этого?И во-вторых, как я могу проверить это до развертывания?Я не могу воспроизвести ошибку пользователя.
public static void InsertIndividualOwner(TaxInfo taxInfo, OwnerAddress address, OwnerEmailAddress emailAddress, IndividualOwner owner)
{
int ownerTaxInfoID = 0;
int addressRecord = 0;
int emailRecord = 0;
int ownerRecord = 0;
int specialistID = Properties.Settings.Default.DefaultUserId;
string insertTaxInfoString = "Insert Into TaxInfo (TIN, BIRT, CAL, PIN, DateAdded, ModifiedBySpecialistID) " +
"VALUES(@TIN, @BIRT, @CAL, @PIN, Date(), @ModifiedBySpecialistID)";
string insertAddressString = "Insert Into OwnerAddress (ownerTaxInfoID, streetAddress1, streetAddress2, city, stateID, zip, country, DateAdded, ModifiedBySpecialistID) " +
"Values(@ownerTaxInfoID, @streetAddress1, @streetAddress2, @city, @stateID, @zip, @country, Date(), @ModifiedBySpecialistID)";
string insertEmailAddressString = "Insert Into OwnerEmailAddress (ownerTaxInfoID, emailAddress, DateAdded, ModifiedBySpecialistID) Values (@ownerTaxInfoID,@emailAddress, Date(), @ModifiedBySpecialistID)";
string insertOwnerNameString = "Insert Into IndividualOwner (firstName, lastName, ownerTaxInfoID, DateAdded, ModifiedBySpecialistID) " +
"VALUES(@firstName, @lastName, @ownerTaxInfoID, Date(), @ModifiedBySpecialistID)";
string selectID = "Select @@Identity";
using (OleDbConnection connection = new OleDbConnection(Constants.ACCESSCONNECTIONSTRING))
{
connection.Open();
using (OleDbTransaction transaction = connection.BeginTransaction())
{
try
{
using (OleDbCommand insertTaxInfo = new OleDbCommand(insertTaxInfoString, connection, transaction))
{
insertTaxInfo.Parameters.AddWithValue("@TIN", taxInfo.tin);
insertTaxInfo.Parameters.AddWithValue("@BIRT", taxInfo.birtNo);
if (string.IsNullOrEmpty(taxInfo.cal))
{
insertTaxInfo.Parameters.AddWithValue("@CAL", DBNull.Value);
}
else insertTaxInfo.Parameters.AddWithValue("@CAL", taxInfo.cal);
if (string.IsNullOrEmpty(taxInfo.pin))
{
insertTaxInfo.Parameters.AddWithValue("@PIN", DBNull.Value);
}
else insertTaxInfo.Parameters.AddWithValue("@PIN", taxInfo.cal);
insertTaxInfo.Parameters.AddWithValue("@ModifiedBySpecialistID", specialistID);
try
{
insertTaxInfo.ExecuteNonQuery();
insertTaxInfo.CommandText = selectID;
ownerTaxInfoID = (int)insertTaxInfo.ExecuteScalar();
}
catch (OleDbException ex)
{
throw ex;
}
}
using (OleDbCommand insertAddress = new OleDbCommand(insertAddressString, connection, transaction))
{
insertAddress.Parameters.AddWithValue("@ownerTaxInfoID", ownerTaxInfoID);
insertAddress.Parameters.AddWithValue("@streetAddress1", address.streetAddress1);
if (address.streetAddress2 == "")
{
insertAddress.Parameters.AddWithValue("@streetAddress2", DBNull.Value);
}
else insertAddress.Parameters.AddWithValue("@streetAddress2", address.streetAddress2);
insertAddress.Parameters.AddWithValue("@city", address.city);
insertAddress.Parameters.AddWithValue("@stateID", address.stateID);
insertAddress.Parameters.AddWithValue("@zip", address.zip);
insertAddress.Parameters.AddWithValue("@country", address.country);
insertAddress.Parameters.AddWithValue("@ModifiedBySpecialistID", specialistID);
try
{
insertAddress.ExecuteNonQuery();
insertAddress.CommandText = selectID;
addressRecord = (int)insertAddress.ExecuteScalar();
}
catch (OleDbException ex)
{
throw ex;
}
}
using (OleDbCommand insertEmailAddress = new OleDbCommand(insertEmailAddressString, connection, transaction))
{
insertEmailAddress.Parameters.AddWithValue("@ownerTaxInfoID", ownerTaxInfoID);
insertEmailAddress.Parameters.AddWithValue("@emailAddress", emailAddress.emailAddress);
insertEmailAddress.Parameters.AddWithValue("@ModifiedBySpecialistID", specialistID);
try
{
insertEmailAddress.ExecuteNonQuery();
insertEmailAddress.CommandText = selectID;
emailRecord = (int)insertEmailAddress.ExecuteScalar();
}
catch (OleDbException ex)
{
throw ex;
}
}
using (OleDbCommand insertOwner = new OleDbCommand(insertOwnerNameString, connection, transaction))
{
insertOwner.Parameters.AddWithValue("@firstName", owner.firstName);
insertOwner.Parameters.AddWithValue("@lastName", owner.lastName);
insertOwner.Parameters.AddWithValue("@ownerTaxInfoID", ownerTaxInfoID);
insertOwner.Parameters.AddWithValue("@ModifiedBySpecialistID", specialistID);
try
{
insertOwner.ExecuteNonQuery();
insertOwner.CommandText = selectID;
ownerRecord = (int)insertOwner.ExecuteScalar();
}
catch (OleDbException ex)
{
throw ex;
}
}
transaction.Commit();
AddOwner?.Invoke();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}
}
}