所以,我有这个我必须维护的内部数据访问框架,我要向它添加事务。我在放入一个包时遇到问题:事务、SqlDataAdapters、SqlCommandBuilders 和主键定义为自动增量的表。
这是我到目前为止所拥有的
public int PutEntity(DaoContext ctx, bool useTransaction=false)
{
int returnValue=ErrorCodes.ERR_SUCCESS;
{
try
{
using (ctx.Connection = DBUtil.GetSqlConnection(ctx.AppCode, this.DBName))
{
SqlDataAdapter adapter = new SqlDataAdapter(BuildSelect(), ctx.Connection);
adapter.FillSchema(ctx.Data.Tables[MainTableName], SchemaType.Source);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(adapter);
if (!useTransaction & ctx.Transaction == null) {
sqlBuilder.GetUpdateCommand();
ctx.Transaction = ctx.Connection.BeginTransaction();
IsTransactionOwner = true;
}
sqlBuilder.GetInsertCommand().Transaction = ctx.Transaction;
sqlBuilder.GetUpdateCommand().Transaction = ctx.Transaction;
sqlBuilder.GetDeleteCommand().Transaction = ctx.Transaction;
adapter.Update(ctx.Data, MainTableName);
ctx.ErrorCode = ErrorCodes.ERR_SUCCESS;
if (!useTransaction & IsTransactionOwner)
{
ctx.Transaction.Commit();
}
return ctx.ErrorCode; //-->
}
}
catch (Exception ex)
{
if (!useTransaction & IsTransactionOwner)
{
ctx.Transaction.Rollback();
}
ex = ErrorHandler.OnError("Error saving data. Record changed while in edit.", ex,
ErrorCodes.ErrorConsequence.None,
ErrorCodes.ERR_DBEXE_PUT_CONCURRENCY,
Thread.CurrentThread.ManagedThreadId,
"application : " + ctx.AppCode,
"on database: " + this.DBName,
ErrorCodes.ERR_DBEXE_PUT_CONCURRENCY_MSG,
"Frw.Common.BizEntity",
"PutEntity()"
);
}
//finally{
// if (!useTransaction & IsTransactionOwner)
// {
// ctx.Transaction.Dispose();
// ctx.Transaction=null;
// }
//}
}
}
我的问题是,当我尝试插入数据时,它永远不会被插入,而当我检查数据集时,它们永远不会有新的自动增量键。我添加的新记录始终为 1