Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>based on my answer on <a href="https://stackoverflow.com/questions/1599230/anyway-see-why-i-get-this-concurrency-violation-in-these-few-lines-of-code/1604547#1604547">concurrency violation</a>, use da.InsertCommand.<strong>UpdatedRowSource</strong> = UpdateRowSource.FirstReturnedRecord.</p> <p><strong>note:</strong> just change SQLiteConnection and SQLiteDataAdapter to MSSQL ones, and change the LAST_INSERT_ROWID() to <strong>SCOPE_IDENTITY</strong>()</p> <pre><code> const string devMachine = @"Data Source=C:\_DEVELOPMENT\__.NET\dotNetSnippets\Mine\TestSqlite\test.s3db"; SQLiteConnection c = new SQLiteConnection(devMachine); SQLiteDataAdapter da = new SQLiteDataAdapter(); DataTable dt = new DataTable(); public Form1() { InitializeComponent(); da = new SQLiteDataAdapter("select product_id, product_name, abbrev from product", c); var b = new SQLiteCommandBuilder(da); da.InsertCommand = new SQLiteCommand( @"insert into product(product_id, product_name, abbrev) values(:_product_id, :_product_name, :_abbrev); select product_id /* include rowversion field here if you need */ from product where product_id = LAST_INSERT_ROWID();", c); da.InsertCommand.Parameters.Add("_product_id", DbType.Int32,0,"product_id"); da.InsertCommand.Parameters.Add("_product_name", DbType.String, 0, "product_name"); da.InsertCommand.Parameters.Add("_abbrev", DbType.String, 0, "abbrev"); da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord; da.UpdateCommand = b.GetUpdateCommand(); da.DeleteCommand = b.GetDeleteCommand(); da.Fill(dt); bds.DataSource = dt; grd.DataSource = bds; } private void uxUpdate_Click(object sender, EventArgs e) { da.Update(dt); } </code></pre> <p>here's the sample table on SQLite:</p> <pre><code>CREATE TABLE [product] ( [product_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [product_name] TEXT NOT NULL, [abbrev] TEXT NOT NULL ) </code></pre> <p><strong>[EDIT Nov 19, 2009 12:58 PM CN]</strong> Hmm... I guess my answer cannot be used, <a href="http://etutorials.org/Programming/building+solutions+with+the+microsoft+net+compact+framework/Part+II+Essential+Architectural+Concepts/Chapter+5.+Caching+Data+with+SQL+Server+CE/Accessing+SQLCE/" rel="nofollow noreferrer">SQLCE does not allow multiple statements</a>.</p> <p>anyway, just use my answer when you use server-based MSSQL or if you use SQLite. or perhaps, encapsulate the two statements to a function that returns scope_identity(integer):</p> <pre><code>da.InsertCommand = new SQLiteCommand( @"select insert_to_product(:_product_id, :_product_name, :_abbrev) as product_id", c); da.InsertCommand.Parameters.Add("_product_id", DbType.Int32,0,"product_id"); da.InsertCommand.Parameters.Add("_product_name", DbType.String, 0, "product_name"); da.InsertCommand.Parameters.Add("_abbrev", DbType.String, 0, "abbrev"); da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload