如何将命令/SQL 语句添加到强类型 TableAdapter 的更新/插入命令?

发布于 2024-08-19 07:10:23 字数 676 浏览 3 评论 0原文

请参阅此问题。我有以下使用强类型数据集针对 SQLite 数据库执行的代码。

        messagesAdapter.Update(messages);//messages is a DataTable
        var connection = messagesAdapter.Connection;
        var retrieveIndexCommand= connection.CreateCommand();
        retrieveIndexCommand.CommandText = @"Select last_insert_rowid()";
        connection.Open();
        var index = retrieveIndexCommand.ExecuteScalar();
        connection.Close();

这不起作用,因为 last_inser_rowid() 始终返回零。这是因为需要在 TableAdapter 的 Update 命令使用的同一连接期间调用它。如何更改 TableAdapter 的插入或更新命令以使其返回索引?

See this question. I have the following code that executes against a SQLIte database using a strongly typed dataset.

        messagesAdapter.Update(messages);//messages is a DataTable
        var connection = messagesAdapter.Connection;
        var retrieveIndexCommand= connection.CreateCommand();
        retrieveIndexCommand.CommandText = @"Select last_insert_rowid()";
        connection.Open();
        var index = retrieveIndexCommand.ExecuteScalar();
        connection.Close();

This does not work as the last_inser_rowid() always returns zero. This caused by the fact that it needs to be called during the same connection that is used by the TableAdapter's Update command. How can I change the the TableAdapter's Insert or Update command so that it return the index?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-08-26 07:10:24

如果您要插入单行,您可以使用它:

// cast if necessary
using (var insert = (SQLiteCommand)this.Adapter.InsertCommand.Clone()) { 
    insert.CommandText += "; SELECT last_insert_rowid()";
    foreach (SQLiteParameter parameter in insert.Parameters) {
        parameter.Value = row[parameter.SourceColumn];
    }
}
var index = Convert.ToInt32(insert.ExecuteScalar());

您还可以使用它插入多行并将您的 id 分配给每一行:

using (var insert = (SQLiteCommand)this.Adapter.InsertCommand.Clone())
{
    insert.CommandText += "; SELECT last_insert_rowid()";
    // this filter only added rows
    foreach (MyDataSet.MessageRow row in messages.GetChanges(DataRowState.Added))
    {
        foreach (SQLiteParameter parameter in insert.Parameters)
        {
            parameter.Value = row[parameter.SourceColumn];
        }
        // use the name of your rowid column
        row.ID = Convert.ToInt32(insert.ExecuteScalar());
        row.AcceptChanges();
    }
}
// then you can perfom the other updates
messagesAdapter.Update(messages);

注意:请务必打开/关闭您的连接

If you are inserting a single row, you can use this:

// cast if necessary
using (var insert = (SQLiteCommand)this.Adapter.InsertCommand.Clone()) { 
    insert.CommandText += "; SELECT last_insert_rowid()";
    foreach (SQLiteParameter parameter in insert.Parameters) {
        parameter.Value = row[parameter.SourceColumn];
    }
}
var index = Convert.ToInt32(insert.ExecuteScalar());

You can also use it to insert multiple rows and assign your id to each one:

using (var insert = (SQLiteCommand)this.Adapter.InsertCommand.Clone())
{
    insert.CommandText += "; SELECT last_insert_rowid()";
    // this filter only added rows
    foreach (MyDataSet.MessageRow row in messages.GetChanges(DataRowState.Added))
    {
        foreach (SQLiteParameter parameter in insert.Parameters)
        {
            parameter.Value = row[parameter.SourceColumn];
        }
        // use the name of your rowid column
        row.ID = Convert.ToInt32(insert.ExecuteScalar());
        row.AcceptChanges();
    }
}
// then you can perfom the other updates
messagesAdapter.Update(messages);

Note: Be sure to open / close your connection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文