DataAdapter.Update不更新表 [英] DataAdapter.Update does not update table

查看:83
本文介绍了DataAdapter.Update不更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的问题很常见,但我没有找到任何解决方案。这是我的代码:

my problem is very common, but I have not found any solution. This is my code:

public async Task<QueryResult> RollbackQuery(ActionLog action)
{
    var inputParameters = JsonConvert.DeserializeObject<Parameter[]>(action.Values);
    var data = DeserailizeByteArrayToDataSet(action.RollBackData);

    using (var structure = PrepareStructure(action.Query, action.Query.DataBase, inputParameters))
    {
        //_queryPlanner is the implementor for my interface
        return await _queryPlanner.RollbackQuery(structure, data);
    }
}



我需要加载DataTable(来自whereever)并将数据替换为数据库。这是我的回滚函数。这个函数使用了一个"CommandStructure",我已经封装了所有的SqlClient对象。 PrepareStructure初始化所有对象

I need to load DataTable (from whereever) and replace data to database. This is my Rollback function. This function use a "CommandStructure" where I've incapsulated all SqlClient objects. PrepareStructure initialize all objects

//_dataLayer is an Helper for create System.Data.SqlClient objects
//ex: _dataLayer.CreateCommand(preSelect) => new SqlCommand(preSelect)
private CommandStructure PrepareStructure(string sql, string preSelect, DataBase db, IEnumerable<Parameter> inputParameters)
{
    var parameters = inputParameters as IList<Parameter> ?? inputParameters.ToList();

    var structure = new CommandStructure(_logger);
    structure.Connection = _dataLayer.ConnectToDatabase(db);
    structure.SqlCommand = _dataLayer.CreateCommand(sql);
    structure.PreSelectCommand = _dataLayer.CreateCommand(preSelect);
    structure.QueryParameters = _dataLayer.CreateParemeters(parameters);
    structure.WhereParameters = _dataLayer.CreateParemeters(parameters.Where(p => p.IsWhereClause.HasValue && p.IsWhereClause.Value));
    structure.CommandBuilder = _dataLayer.CreateCommandBuilder();
    structure.DataAdapter = new SqlDataAdapter();

    return structure;
}



因此,我的函数使用SqlCommandBuilder和DataAdapter对Database进行操作.PreSelectCommand类似于"Select * from Purchase where CustomerId = @ id"表Purchase在ID上有一个primaryKey

So, my function uses SqlCommandBuilder and DataAdapter to operate on Database. PreSelectCommand is like "Select * from Purchase where CustomerId = @id" The table Purchase has one primaryKey on ID filed

public virtual async Task<QueryResult> RollbackQuery(CommandStructure cmd, DataTable oldData)
{
    await cmd.OpenConnectionAsync();

    int record = 0;
    using (var cmdPre = cmd.PreSelectCommand as SqlCommand)
    using (var dataAdapt = new SqlDataAdapter(cmdPre))
    using (var cmdBuilder = new SqlCommandBuilder(dataAdapt))
    {
        dataAdapt.UpdateCommand = cmdBuilder.GetUpdateCommand();
        dataAdapt.DeleteCommand = cmdBuilder.GetDeleteCommand();
        dataAdapt.InsertCommand = cmdBuilder.GetInsertCommand();

        await cmd.OpenConnectionAsync();
using (var tbl = new DataTable(oldData.TableName)) { dataAdapt.Fill(tbl); dataAdapt.FillSchema(tbl, SchemaType.Source); tbl.Merge(oldData); foreach (DataRow row in tbl.Rows)
{ try { row.SetModified(); } catch { } } record = dataAdapt.Update(tbl); } } return new QueryResult { RecordAffected = record }; }






 

 


 

 


。 



I执行代码,我没有任何错误,但数据没有更新。

变量"记录"包含正确的数字修改(??)记录,但.....在桌子上什么都没有

I Execute the code and I don't have any errors, but the data are not updated.
Variable "record" contain the right number of modified (??) record, but..... on the table nothing


有人可以帮帮我吗?

can someone help me?


编辑

EDIT:

现在我做了一处更改:

tbl.Merge(oldData) => tbl.Merge(oldData, true)


所以我看到执行预期的查询但是,反向参数。

so I see perform the expected query but, with reversed parameters.

UPDATE购买SET价格= 123,其中id = 6,价格= 22

UPDATE Purchase SET price=123 where id=6 and price=22

而不是

更新购买SET价格= 22其中id = 6且price = 123


Programamtore ASP.NET

http://glucolo.wordpress。 com

Programamtore ASP.NET
http://glucolo.wordpress.com

推荐答案

如果此项目基于本地数据库文件而不是SQL Server,则检查文件夹。有时,用于调试目的,数据库文件由Visual Studio复制,因此您不会在原始
数据库中看到更改。

你是如何检查数据库表中的结果的?


这篇关于DataAdapter.Update不更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆