所以,我有这个内部数据访问框架,我必须维护,我要添加交易。我在放入一个包时遇到问题:事务,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
答案 0 :(得分:0)
我找到了问题的解决方案。它涉及从SqlCommandBuilder中提取SqlCommands并更改它们,这些内容在
行中 SqlDataAdapter daAutoNum = new SqlDataAdapter();
using (ctx.Connection = DBUtil.GetSqlConnection(ctx.AppCode, this.DBName))
{
SqlDataAdapter adapter = new SqlDataAdapter(BuildSelect(), ctx.Connection);
SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(adapter);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
adapter.UpdateCommand = sqlBuilder.GetUpdateCommand();
adapter.InsertCommand = sqlBuilder.GetInsertCommand();
string scope_id = string.Format(" ; select {0} from {1} where {0} = SCOPE_IDENTITY();", PrimaryKeyName, MainTableName);
adapter.InsertCommand.CommandText += scope_id;
adapter.DeleteCommand = sqlBuilder.GetDeleteCommand();
SqlParameter identParam = new SqlParameter("@Identity", PrimaryKeyType, 0, PrimaryKeyName);
identParam.Direction = ParameterDirection.Output;
adapter.InsertCommand.Parameters.Add(identParam);
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
daAutoNum.DeleteCommand = adapter.DeleteCommand;
daAutoNum.InsertCommand = adapter.InsertCommand;
daAutoNum.UpdateCommand = adapter.UpdateCommand;
daAutoNum.InsertCommand.Transaction = ctx.Transaction;
daAutoNum.DeleteCommand.Transaction = ctx.Transaction;
daAutoNum.UpdateCommand.Transaction = ctx.Transaction;
daAutoNum.Update(ctx.Data, MainTableName);
}