gpt4 book ai didi

c# - 如何使用自动增量键和 SqlDataAdapter 来添加数据库新行

转载 作者:行者123 更新时间:2023-11-29 23:59:42 29 4
gpt4 key购买 nike

所以,我有一个必须维护的内部数据访问框架,并且我要向其中添加事务。我在放入一个包时遇到问题:Transactions、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

最佳答案

我找到了问题的解决方案。它涉及从 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);

}

关于c# - 如何使用自动增量键和 SqlDataAdapter 来添加数据库新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25121647/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com