反正看到为什么我得到这个“并发冲突”在这几行代码中???并发冲突:UpdateCommand影响预期的1个记录的0 [英] anyway see why I get this "Concurrency Violation" in these few lines of code??? Concurrency violation: the UpdateCommand affected 0 of the expected 1 records

查看:137
本文介绍了反正看到为什么我得到这个“并发冲突”在这几行代码中???并发冲突:UpdateCommand影响预期的1个记录的0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,任何想法为什么我得到这个错误?

Here is the code, any ideas why I get this error?

private SQLiteDataAdapter DA_Webfiles;
// Setup connection, fill dataset etc

DataTable dt = this.dataSet.Tables["WEBFILES"];
DataRow newRow = dt.NewRow();
newRow["PATH"] = _url;
dt.Rows.Add(newRow);
this.DA_Webfiles.Update(this.dataSet, "WEBFILES");
// Works to Here

newRow["CONTENT_TYPE"] = "Test Content Type";
this.DA_Webfiles.Update(this.dataSet, "WEBFILES");
// Get ERROR here - Concurrency violation: the UpdateCommand affected 0 of the expected 1 records

谢谢

推荐答案

你需要:dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

you need: dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

得到代码线索:检索身份或自动编号值(ADO.NET )

表:

CREATE TABLE [emp] (
[emp_id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,
[emp_firstname] VARCHAR(100) NOT NULL,
[emp_lastname] varchar(100) not null
)

代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


        var c = Connect();

        var da = new SQLiteDataAdapter("select emp_id, emp_firstname, emp_lastname from emp where 1 = 0", c);



        var b = new SQLiteCommandBuilder(da);

        da.InsertCommand = new SQLiteCommand(
            @"insert into emp(emp_firstname, emp_lastname ) values(:_emp_firstname, :_emp_lastname);
            select emp_id /* include rowversion field here if you need */ from emp where emp_id = last_insert_rowid();", c);
        da.InsertCommand.Parameters.Add("_emp_firstname", DbType.String, 0, "emp_firstname");
        da.InsertCommand.Parameters.Add("_emp_lastname", DbType.String, 0, "emp_lastname");
        da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

        da.UpdateCommand = b.GetUpdateCommand();
        da.DeleteCommand = b.GetDeleteCommand();


        var dt = new DataTable();
        da.Fill(dt);

        var nr = dt.NewRow();
        nr["emp_firstname"] = "john";
        nr["emp_lastname"] = "lennon";

        dt.Rows.Add(nr);

        da.Update(dt);

        dt.AcceptChanges();

        nr["emp_lastname"] = "valjean";
        da.Update(dt);

    }

    SQLiteConnection Connect()
    {
        return new SQLiteConnection(@"Data Source=../../test.s3db;Version=3;");
    }
}

上面的代码也可以用于多插入。概念验证码:

the code above works on multi-insert too. proof-of-concept code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


        var c = Connect();

        var da = new SQLiteDataAdapter("select emp_id, emp_firstname, emp_lastname from emp where 1 = 0", c);



        var b = new SQLiteCommandBuilder(da);

        da.InsertCommand = new SQLiteCommand(
            @"insert into emp(emp_firstname, emp_lastname ) values(:_emp_firstname, :_emp_lastname);
            select emp_id /* include rowversion field here if you need */ from emp where emp_id = last_insert_rowid();", c);
        da.InsertCommand.Parameters.Add("_emp_firstname", DbType.String, 0, "emp_firstname");
        da.InsertCommand.Parameters.Add("_emp_lastname", DbType.String, 0, "emp_lastname");
        da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

        da.UpdateCommand = b.GetUpdateCommand();
        da.DeleteCommand = b.GetDeleteCommand();


        var dt = new DataTable();
        da.Fill(dt);

        var nr = dt.NewRow();
        nr["emp_firstname"] = "john";
        nr["emp_lastname"] = "lennon";


        var nrx = dt.NewRow();
        nrx["emp_firstname"] = "paul";
        nrx["emp_lastname"] = "mccartney";


        dt.Rows.Add(nr);
        dt.Rows.Add(nrx);

        da.Update(dt);

        dt.AcceptChanges();


        nrx["emp_lastname"] = "simon";
        da.Update(dt);

        nr["emp_lastname"] = "valjean";
        da.Update(dt);

    }

    SQLiteConnection Connect()
    {
        return new SQLiteConnection(@"Data Source=../../test.s3db;Version=3;");
    }
}

这篇关于反正看到为什么我得到这个“并发冲突”在这几行代码中???并发冲突:UpdateCommand影响预期的1个记录的0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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